I would like to map between UserDTO
and User
, but excluding one field, say city
. How can I do that, cause I though that this approach would work, but it doesn't:
ModelMapper modelMapper = new ModelMapper();
modelMapper.typeMap(UserDTO.class,User.class).addMappings(mp -> {
mp.skip(User::setCity);
});
Because of the generic parameters, we couldn't use the lambda expression.
ModelMapper modelMapper = new ModelMapper();
modelMapper.addMappings(new PropertyMap<Dto, Source>() {
@Override
protected void configure() {
skip(destination.getBlessedField());
}
});
For the configuration to work need to add:
modelMapper.getConfiguration().setAmbiguityIgnored(true);
E.g.
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setAmbiguityIgnored(true);
modelMapper.addMappings(clientPropertyMap);
modelMapper.map(UserDTO, User);
PropertyMap<UserDTO, User> clientPropertyMap = new PropertyMap<UserDTO, User>() {
@Override
protected void configure() {
skip(destination.getCity());
}
};
For the configuration to work need to add:
modelMapper.getConfiguration().setAmbiguityIgnored(true);
This is true only when the destination field matches to multiple source fields. Skipping the setting of a destination field will work without the above if there is either a 1-1 or a 0-1 match between source-destination.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With