Mapper configuration

@Component public class ModelMapperFactoryBean implements FactoryBean<ModelMapper> { @Autowired private Collection<MapperConfigurer> configurers; @Override public ModelMapper getObject() { final ModelMapper modelMapper = new ModelMapper(); if (!CollectionUtils.isEmpty(configurers)) { configurers.forEach(c -> c.configure(modelMapper)); } return modelMapper; } } 

Converter example

  @Component public class ModelConverter implements MapperConfigurer { @Override public void configure(ModelMapper modelMapper) { modelMapper.addConverter(new AbstractConverter<ModelEntity, Model>() { @Override protected Model convert(ModelEntity source) { return Model.builder() .id(source.getId()) .name(source.getName()) .serverConfig(modelMapper.map(source.getServerConfig(), ServerProcessConfig.class)) .build(); } }); } } 

Spring application in which ipolzu mapper. For each entity there is a converter. There are for Model. there is also for ServerProcessConfig.

When converting a model, I call the modelmapper and call its map method to convert the nested object, for which there is also an implemented converter.

Accordingly, I expect the mapper to be used by him. However, obviously the mapper does not use it (the breakpoint does not work) but tries to transform the object in this way, apparently, simply by matching the fields.

If you simply try (not as an attached object) to convert the ServerProcessConfig, then the converter implemented for this will work as expected.

Why doesn't the mapper use other converters for nested objects?

  • And you in the mapper zaregali these converter? - Tsyklop
  • of course. I clarified in the description that if you convert ServerProcessConfig directly (not as an embedded object), then everything goes as it should - the correct converter is used. - Konstantin Pl

0