MapStruct Mapper as Spring Framework Converter - idiomatic use possible?
It's been more than a year since I asked this question, but now we've come up with an answer inside the MapStruct project itself - the MapStruct Spring Extensions project.
A CarMapper
example is provided as an example within the project.
You can just skip passing a WheelMapper
entirely, when you just have a CarMapper
the generated CarMapperImpl
will contain a logic to map Wheel
<-> WheelDto
as well. No need to pass anything to uses
, making your issue obsolete.
carDto.setWheel( wheelToWheelDto( car.getWheel() ) );
with a method like;
protected WheelDto wheelToWheelDto(Wheel wheel) {
if ( wheel == null ) {
return null;
}
WheelDto wheelDto = new WheelDto();
wheelDto.setName( wheel.getName() );
return wheelDto;
}
I did try to achieve an intelligent injection of ConversionService
through MapStruct
, but it is not possible I think. You'd need support from MapStruct
to achieve such a feat. It does not even consider injecting ConversionService
. Maybe a custom generic mapper that is already implemented and uses ConversionService
might work, but I was unable to do that! Though I don't see any reason for it since MapStruct
is already creating all necessary smaller mappers from the parent mapper...