0

I have this problem, this is my DTO:

public class OrderDTO {
    private Long id;
    @JsonProperty("_embedded")
    private OrderEmbeddedListJsonDTO embedded;
}

Here my other DTO:

public class OrderEmbeddedListJsonDTO {
    private List<Order> order;
}

But I need map OrderEmbeddedListJsonDTO propertie since OrderDTO for example:

orderDTOMapped.setEmbedded(order.getEmbedded(HERE NEED: setStartAddress("new value")));

This is the class of orderDTOMapped:

public OrderWithAddressDTO orderToOrderDTO(OrderDTO order) throws OrderGetException {
        try {
                OrderWithAddressDTO orderDTOMapped = new OrderWithAddressDTO();
                orderDTOMapped.setId(order.getId());
                orderDTOMapped.setEmbedded(order.getEmbedded());
            return orderDTOMapped;
        }  catch (Exception e) {
            throw new OrderGetException();
        }
    };

And order is a entity:

public class Order implements java.io.Serializable {

    private Long id;
    private Double latitude;
    private Double longitude;
    @JsonProperty("_embedded")
    private OrderEmbeddedListJsonDTO embedded;
}

How can do this? Could you help me please?

Updated:

I was able to fix it with this code:

List<Order> shippableOrders = order.getEmbedded()
                        .getOrder()
                        .stream()
                        .map(this::mapOrderToShippableOrder)
                        .collect(Collectors.toList());
                OrderEmbeddedListJsonDTO ordersList = new OrderEmbeddedListJsonDTO();
                ordersList.setOrder(shippableOrders);
                orderDTOMapped.setEmbedded(ordersList);
                return orderDTOMapped;

I appreciate your help, thanks for
tmarwen

  • Could you rephrase your post? It is clear what you are trying to achieve. What does the `order` refer to and what is the class declaring the `setStartAddress` setter? – tmarwen Oct 16 '21 at 17:17
  • Doesn't the `orderDTOMapped.setEmbedded(order.getEmbedded())` produce the required results and maps your data from the `OrderDTA` to the `OrderWithAddressDTO` type? – tmarwen Oct 16 '21 at 17:41
  • Yes, but I need create new data, for example setNewAddress("address-here") and include the actual data – Johan Smith Oct 16 '21 at 17:47
  • Where does the `"address-here"` will come from? – tmarwen Oct 16 '21 at 17:58
  • This is a result of request in another endpoint with parameters to actual orderDTO. I need join those data in the only newDTO – Johan Smith Oct 16 '21 at 18:07
  • I am really sorry but I doubt anyone could be of help if you do not restructure the issue. You describe the input (being either you DTO or domain types), what data to they include then describe the exact shape of the output and its shape and what each of these fields is expected to have its values mapped from. – tmarwen Oct 16 '21 at 18:13
  • This is the result, and I need add new data: { "_embedded": { "order": [ { "id": 12, "startLatitude": 4.498942417626324, "startLongitude": -75.44666 } ] } } – Johan Smith Oct 16 '21 at 18:19
  • The problem is in the _embedded I cant do setNewData() there – Johan Smith Oct 16 '21 at 18:19

1 Answers1

0

You can iterate over the OrderDTO#embedded items mapping each items to a new instances copying / replacing whatever data you need:

public OrderWithAddressDTO orderToOrderDTO(OrderDTO order) throws OrderGetException {
    try {
        OrderWithAddressDTO orderDTOMapped = new OrderWithAddressDTO();
        orderDTOMapped.setId(order.getId());
        List<Order> shippableOrders = order.getEmbedded()
                .getOrder()
                .stream()
                .map(this::mapOrderToShippableOrder)
                 .collect(Collectors.toList());
        OrderEmbeddedListJsonDTO ordersList = new OrderEmbeddedListJsonDTO();
        ordersList.setEmbedded(ordersList);
        return orderDTOMapped;
    }  catch (Exception e) {
        throw new OrderGetException();
    }
};

private Order mapOrderToShippableOrder(Order order) {
    Order result = new Order();
    // map fields from `order` to `result` order including `new-address`
    return result;
}
tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • Thanks men! This looks better like answer, but I have this error: Required type: OrderEmbeddedListJsonDTO Provided: List I supposed this is by my entity class have declared: private OrderEmbeddedListJsonDTO embedded; How I can fix this error message? – Johan Smith Oct 16 '21 at 21:54
  • I use orderDTOMapped.getEmbedded().setOrder(order .getEmbedded() .getOrder() And finally works for me! You are the best man!! Thanks infinite – Johan Smith Oct 16 '21 at 22:02
  • I have updated the answer adding the intermediary needed type. – tmarwen Oct 16 '21 at 22:07