API request - adding to grid

Currently I have a grid component. I am adding columns in this manner:

gridSerial.addColumn(serialRoot -> {
                SerialNumbers serialNumbers = serialRoot.getSerial_numbers().get(0);
                return serialNumbers.getOrder_number();
            }).setHeader("Order Number");```

The problem with this is `.get(0)`. I need to not display just one order number, but all order numbers from the api request. What is the best way to do this?

```java
public SerialRoot getSerialRoot() {
        return webClient
                .get()
                .uri("/order/get_scanned_serial_numbers?start=2021-11-29T00:00:00Z&end=%s".formatted(date))
                .header(auth, skuT + token)
                .retrieve()
                .bodyToMono(SerialRoot.class)
                .block();
    }```

API request - adding to grid

Any idea on this?

Can you please elaborate on what you want to achieve? Do you want to show all order numbers in the same cell or have a different row for each of them?

Different row for each

You may need to create another class with only the attributes you need to show on the grid, and then transform SerialRoot, for example by using a stream and flatMap over getSerial_numbers collection

So I created a DTO to handle the attributes I needed. In my service class, wouuld I use the DTO, or would I keep it as Serial Root?
Before:

public SerialRoot getSerialNums() {
        return webClient
                .get()
                .uri("/order/get_scanned_serial_numbers?start=2021-11-29T00:00:00Z&end=%s".formatted(date))
                .header(auth, skuT + token)
                .retrieve()
                .bodyToMono(SerialRoot.class)
                .block();
    }```

After DTO implementation:
```java
public ApiResponseDTO getSerialNums() {
        return  webClient
                .get()
                .uri("/order/get_scanned_serial_numbers?start=2021-11-29T00:00:00Z&end=%s".formatted(date))
                .header(auth, skuT + token)
                .accept(MediaType.ALL)
                .retrieve()
                .bodyToMono(ApiResponseDTO.class)
                .block();
    }```

Currently, I am getting a bean error in my List class:
`There was an exception while trying to navigate to '' with the root cause 'java.lang.ClassCastException: class com.example.demo.model.serial.SerialRoot cannot be cast to class java.util.stream.Stream (com.example.demo.model.serial.SerialRoot is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @774aab9c; java.util.stream.Stream is in module java.base of loader 'bootstrap')'`

My list class is as follows with DTO:
```java
 public OrderList(SerialsService service) {
      Grid<ApiResponseDTO> grid = new Grid<>(ApiResponseDTO.class);
      grid.setColumns();
      grid.addColumn(ApiResponseDTO::getSerial_number).setHeader("Serial");
      grid.setItems((Stream<ApiResponseDTO>) service.getSerialNums());
}```

service.getSerialNums returns a ApiResponseDTO object, you cannot cast it to a stream

Removing the stream, I get:
There was an exception while trying to navigate to '' with the root cause 'org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json;charset=utf-8' not supported for bodyType=com.example.demo.dto.ApiResponseDTO'

I don’t know what api you are using to retrieve data from remote service, and neither what the response look like
Basically you have to transform the output of the json response in a list of dto items

Response is fairly simple,

{
    "serial_numbers": [
        {
            "_id": "632c8c14a8412d2c26a28c64",
            "store_id": "62cef3d67076781bae25ae6c",
            "order_number": "E1163",
            "item_id": "62cef3ea62c214aa81912cd4",
            "time": "2022-09-22T16:27:41.857Z",
            "serial_number": "Serial #: 110358140",
            "listing_id": "6720716865704",
            "variant_id": "39865161154728"
        }
    ]
}```

So you can just return SerialRoot from the service and have the grid using SerialNumbers class.
You will pass SerialRoot. getSerial_numbers to grid.setItems

I tried that, it asks me to stream ApiResponseDTO. When I stream it, it throws an error still telling me items is null.

There was an exception while trying to navigate to '' with the root cause 'java.lang.NullPointerException: Cannot invoke "java.util.stream.Stream.collect(java.util.stream.Collector)" because "items" is null'

I don’t think im setting DTO up properly…

Based on the response you posted you might not need the dto, since I suppose the SerialRoot class contains only the serial numbers collection

public class SerialRoot {

    @JsonProperty("serial_numbers")
    @Bean
    public ArrayList<SerialNumbers> getSerial_numbers() {
        return this.serial_numbers; }
    public void setSerial_numbers(ArrayList<SerialNumbers> serial_numbers) {
        this.serial_numbers = serial_numbers; }
    ArrayList<SerialNumbers> serial_numbers;

}```
Entire SerialRoot class

Exactly. Just use a Grid and pass then grid.setItems(serialRoot.getSerial_numbers())

BTW, I think the Bean annotation on SerialRoot class does not make any sense

It does not let me pass grid.setItems(SerialRoot.getSerial_numbers())

What error do you have?