Java 如何在 Spring Boot 中使用推土机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28369433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:06:01  来源:igfitidea点击:

How to use Dozer with Spring Boot?

javaspringannotationsdtodozer

提问by Pracede

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.

我正在做一个 Spring Boot 项目。我只有注释配置。我想包括推土机来将实体转换为 DTO,将 DTO 转换为实体。我在推土机网站上看到,他们解释说我必须在 spring xml 配置文件中添加以下配置。由于我没有 xml 文件而是注释配置 Java 类,因此我不知道如何将其转换为 Java 配置类。

<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

如果有人能给我一个例子,那将非常有用。谢谢

采纳答案by micha

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;
  }

  ...
}

回答by bhdrkn

If you are using DozerBeanMapperFactoryBeaninstead of DozerBeanMapperyou may use something like this.

如果你正在使用DozerBeanMapperFactoryBean而不是DozerBeanMapper你可以使用这样的东西。

@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.

这样您就可以自动导入您的映射。比简单地注入您的 Mapper 并使用。

@Autowired
private Mapper mapper;

Update with Dozer 5.5.1

使用推土机 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,

在推土机 5.5.1 中,删除了 DozerBeanMapperFactoryBean。因此,如果您想使用更新版本,则需要执行以下操作,

@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);

例如。mapper.map(admin, UserDTO.class);

回答by Xavier Bouclet

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.

以防万一有人想避免使用 xml dozer 文件。您可以直接在 java 中使用构建器。对我来说,这是在注释 Spring 上下文中使用的方法。

See more information at mapping api dozer

映射 api 推土机上查看更多信息

    @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.

就我而言,它更有效(至少是第一次)。没有做任何基准测试或任何事情。