I would like to map fields from source
into the existing object dest
via custom Converter. Could you please suggest a canonical way to reach out dest
object from AbstractConverter#convert()
Please find the code below:
Source source = new Source(xxx);
Dest dest = new Dest(yyy);
modelMapper.addConverter(new AbstractConverter<Source, Dest>() {
@Override
protected Dest convert(Source source) {
// here I need to access 'dest' object in order to manually map fields from 'source'
});
modelMapper.map(source, dest);
If you want to access the destination object you should not use AbstractConverter, but an anonymous Converter:
modelMapper.addConverter(new Converter<Source, Dest>() {
public Dest convert(MappingContext<Source, Dest> context) {
Source s = context.getSource();
Dest d = context.getDestination();
d.setYyy(s.getXxx() + d.getYyy()); // example of using dest's existing field
return d;
}
});