As you have multiple default methods that return the same type. You would need to use Mapping method selection based on qualifiers.
What this means is that you would need to write your mapper in the following format:
@Mapper
public interface ItemMapper {
// Omitting other mappings for clarity
@Mapping(source = "item", target = "locationDto", qualifiedByName = "locationDto")
@Mapping(source = "item", target = "binType", qualifiedByName = "binType")
@Mapping(source = "item", target = "lotSize", qualifiedByName = "lotSize")
@Mapping(source = "item", target = "stockRails", qualifiedByName = "stockRails")
ItemViewModel itemToDto(Item item);
@Named("locationDto")
default String locationToLocationDto(Item item) {
//Omitting implementation
}
@Named("binType")
default double locationToBinType(Item item) {
//Omitting implementation
}
@Named("lotSize")
default double itemToLotsize(Item item) {
//Omitting implementation
}
@Named("stockRails")
default double stockRails(Item item) {
//Omitting implementation
}
}
Some important notes:
- You need to use
@Named
from the MapStruct package - In
source
you can also specify the name of the parameter of the method - In
qualifiedByName
you need to specify the value that you have written in@Named
- It finds the method not only by the value of
@Named
but also by the parameter type and return type. If it gives a compilation error that it cannot find the method by@Named
it might be because the types of the parameter and the return value of the method do not fit the types of thetarget
and thesource
of the@Mapping
I would strongly advise against using expressions for such complicated things. It is much more difficult to get it correct and it is more difficult for maintaining