ModelMapper skip a field

后端 未结 3 1338
旧巷少年郎
旧巷少年郎 2020-12-30 06:54

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

相关标签:
3条回答
  • 2020-12-30 07:13

    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());
        }
    };
    
    0 讨论(0)
  • 2020-12-30 07:18

    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());
                    }
                });
    
    0 讨论(0)
  • 2020-12-30 07:21

    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.

    0 讨论(0)
提交回复
热议问题