MapStruct add a new calculated field to the dto

Viewed 6641

I'm trying to map an entity Order to a OrderDTO using MapStruct. I want to add to OrderDTO a new field total, this field is not available in the original entity Order and should be calculated using the information available in the Order (order entries price, quantity, taxes...). I created a new field total in the OrderDTO and I'm trying to map it by adding a default method to the mapper interface:

public interface OrderMapper {

    ...

    default BigDecimal orderToTotal(Order order){
        return logicToCalculateTotal();
    }
}

When I lunch the build MapStruct launch the error

Unmapped target property: "total".

Any idea how to solve this problem?

Thanks

2 Answers

if the calculated field can be completely calculated by using other fields in DTO:

I would put these calculations into getMethods - no need to add redundant fields. (Thinking about tight cohesion)

If you name the method getTotal it will be seen in json/xml with name "total" besides all other fields.

Related