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();
}```
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());
}```
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
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'