NullPointerException when trying to define a custom PropertyMap

后端 未结 3 805
灰色年华
灰色年华 2020-12-21 02:01

I\'m using ModelMapper the following way :

I have a few converter classes that are Spring components and they register custom ModelMapper mappings

@C         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-21 02:51

    For anyone coming across with the same issue: My problem was that I used PropertyMap with anonymous implementation like the documentation suggested within a spring configuration. This messed up within the ExplicitMappingBuilder. What I have now is the following:

    @Configuration
    public class ApplicationConfig {
    
        @Bean
        public ModelMapper modelMapper() {
            ModelMapper modelMapper = new ModelMapper();
            modelMapper.addMappings(new UserPropertyMap());
            return modelMapper;
        }
    }
    

    My UserPropertyMap looks like this:

    public class UserPropertyMap extends PropertyMap {
        @Override
        protected void configure() {
            map().setUserName(source.getUsername());
        }
    }
    

    This worked like a charm in Spring Boot 2.

提交回复
热议问题