ModelMapper skip a field

user3529850 picture user3529850 · Mar 2, 2018 · Viewed 18.8k times · Source

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);
});

Answer

Muhammed Ozdogan picture Muhammed Ozdogan · Sep 9, 2018

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());
                }
            });