How to use Dozer with Spring Boot?

前端 未结 3 1213
再見小時候
再見小時候 2021-01-02 02:51

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 web

3条回答
  •  情深已故
    2021-01-02 03:27

    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.

提交回复
热议问题