0

I am working on a spring boot app where I am getting pageable response and then I want to take that pageable response array to parse it to a list of some DTO.

Here's my function:

public Object fetchSkuGroupsByIdAndWarehouseId(Integer warehouseId, Integer pageNumber, Integer pageSize,
                                                   Long groupId) throws IOException, NoSuchFieldException, IllegalAccessException {
        ResponseEntity<FeignClientResponse> responseEntity = locationInventoryClient.fetchGroupsByIdAndWarehouseId(warehouseId,pageNumber,pageSize,groupId);
        PageableResponse response;
        try {
              response= objectMapper.readValue(objectMapper.writeValueAsString(
                    responseEntity.getBody().getData()), PageableResponse.class);
        } catch (JsonProcessingException e){
            log.error("JsonProcessingException in method fetchSkuGroupsByIdIn");
            throw new InvalidMovementRequest("JsonProcessingException in method fetchSkuGroupsByIdIn");
        }
        log.info(response.getData().toString());
        List<Object> list= (List<Object>) response.getData();
        //have to convert here  to List<SkuGroupDTO>.
        
     return "hey";
    }

I am also getting the List list correctly but not able to parse it to a existing DTO that I have,I tried map and streams but no success.

Any ways that I can do it?

rudeTool
  • 526
  • 1
  • 11
  • 25

1 Answers1

0

You can create a mapper who transform your data to your DTO inside the YourDTOMapper class like this:

public static YourDTO mapObjectTo(Object entity) {
    return YourDTO.builder()
            .id(entity.getId())
            .name(entity.getName())
            .build();
}

(replace id and name with your properties) than use it like this:

List<YourDTO> yourDTO = response.getData().stream().map(YourDTOMapper::mapObjectTo)
.collect(Collectors.toList());
lmasneri
  • 589
  • 7
  • 17