A very simple problem which smoked holes in my case. I'm trying to skip userDTO's setPassword every time when I convert User --> UserDTO.
I get NullPointerException, at this line in ModelMapper -
I understand the 'source' mapping is not present but my question is why does it even care about it since I asked it to not set the password at all.
Sorry, I think I'm lacking some basics here with ModelMapper. Thank you for your time and help.
Got help from ModelMapper skip a field and few other links but no luck.
Below is the code
@Bean
public ModelMapper modelMapper() {
ModelMapper mm = new ModelMapper();
mm.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
TypeMap<User, UserDTO> userEntityToDTOMap = mm.createTypeMap(User.class, UserDTO.class);
userEntityToDTOMap.addMappings(a -> a.skip(UserDTO::setPassword));
return mm;
}
public static void main(String[] args) {
BootstrapConfigurationManager mgr = new BootstrapConfigurationManager();
ModelMapper mm = mgr.modelMapper();
mm.getConfiguration().setPropertyCondition(Conditions.isNotNull());
User user = new User();
user.setId(44L);
user.setPassword("password");
UserDTO userDTO = new UserDTO();
userDTO.setEmailAddress("abc@abc.com");
mm.map(user, userDTO);
System.out.println(userDTO.getId());
System.out.println(userDTO.getEmailAddress());
System.out.println(userDTO.getPassword());
}