Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Dozer with Spring Boot?

I am working on a Spring Boot project. I just have annotation configuration. I want to include dozer to transform Entities to DTO and DTO to Entities. I see in the dozer website, they explain i have to add the following configuration in spring xml configuration file. Since i have not xml file but annotation configuration Java class, i don't know how to translate this into Java Configuration class.

<bean id="org.dozer.Mapper" class="org.dozer.DozerBeanMapper">
  <property name="mappingFiles">
    <list>
      <value>dozer-global-configuration.xml</value>
      <value>dozer-bean-mappings.xml</value>
      <value>more-dozer-bean-mappings.xml</value>
    </list>
  </property>
</bean>

If someone could you give me an example it'll be very useful. Thanks

like image 709
Pracede Avatar asked Feb 06 '15 15:02

Pracede


People also ask

What is spring boot Dozer?

Overview. Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another, attribute by attribute. The library not only supports mapping between attribute names of Java Beans, but also automatically converts between types – if they're different.

What is a bean Mapper?

Java Bean Mapper, Open Source. Dozer is a powerful library which can help us in avoiding lots of unnecessary code, while we want to copy data from one bean to another bean. It is mainly bean to bean mapper that recursively copies data from one java object to another java object – attribute by attribute.


3 Answers

If you are using DozerBeanMapperFactoryBean instead of DozerBeanMapper you may use something like this.

@Configuration
public class MappingConfiguration {

    @Bean
    public DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean(@Value("classpath*:mappings/*mappings.xml") Resource[] resources) throws Exception {
        final DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean = new DozerBeanMapperFactoryBean();
        // Other configurations
        dozerBeanMapperFactoryBean.setMappingFiles(resources);
        return dozerBeanMapperFactoryBean;
    }
}

This way you can import your mappings automatically. Than simple inject your Mapper and use.

@Autowired
private Mapper mapper;

Update with Dozer 5.5.1

In dozer 5.5.1, DozerBeanMapperFactoryBean is removed. So if you want to go with an updated version you need do something like below,

@Bean
public Mapper mapper(@Value(value = "classpath*:mappings/*mappings.xml") Resource[] resourceArray) throws IOException {
    List<String> mappingFileUrlList = new ArrayList<>();
    for (Resource resource : resourceArray) {
        mappingFileUrlList.add(String.valueOf(resource.getURL()));
    }
    DozerBeanMapper dozerBeanMapper = new DozerBeanMapper();
    dozerBeanMapper.setMappingFiles(mappingFileUrlList);
    return dozerBeanMapper;
}

Now inject mapper as told above

@Autowired
private Mapper mapper;

And use like below example,

mapper.map(source_object, destination.class);

eg. mapper.map(admin, UserDTO.class);

like image 158
bhdrkn Avatar answered Oct 19 '22 11:10

bhdrkn


I think something like this should work:

@Configuration
public class YourConfiguration {

  @Bean(name = "org.dozer.Mapper")
  public DozerBeanMapper dozerBean() {
    List<String> mappingFiles = Arrays.asList(
      "dozer-global-configuration.xml", 
      "dozer-bean-mappings.xml",
      "more-dozer-bean-mappings.xml"
    );

    DozerBeanMapper dozerBean = new DozerBeanMapper();
    dozerBean.setMappingFiles(mappingFiles);
    return dozerBean;
  }

  ...
}
like image 45
micha Avatar answered Oct 19 '22 13:10

micha


Just in case someone wants to avoid xml dozer file. You can use a builder directly in java. For me it's the way to go in a annotation Spring context.

See more information at mapping api dozer

    @Bean
public DozerBeanMapper mapper() throws Exception {
    DozerBeanMapper mapper = new DozerBeanMapper();
    mapper.addMapping(objectMappingBuilder);
    return mapper;
}

BeanMappingBuilder objectMappingBuilder = new BeanMappingBuilder() {
    @Override
    protected void configure() {
        mapping(Bean1.class, Bean2.class)
                .fields("id", "id").fields("name", "name");
    }
};

In my case it was more efficient (At least the first time). Didn't do any benchmark or anything.

like image 24
Xavier Bouclet Avatar answered Oct 19 '22 12:10

Xavier Bouclet