编程语言
首页 > 编程语言> > java-没有XML的Dozer API日期映射配置

java-没有XML的Dozer API日期映射配置

作者:互联网

有谁知道如何将以下内容转换为推土机的api调用格式?关于Java映射,Dozer映射站点上的文档非常苗条.

   <mappings>
  <configuration>
    <date-format>MM/dd/yyyy HH:mm</date-format>
  </configuration>

  <mapping wildcard="true">
    <class-a>org.dozer.vo.TestObject</class-a>
    <class-b>org.dozer.vo.TestObjectPrime</class-b>
    <field>
      <a>dateString</a>
      <b>dateObject</b>
    </field>
  </mapping>
  <mapping>
    <class-a>org.dozer.vo.SomeObject</class-a>
    <class-b>org.dozer.vo.SomeOtherObject</class-b>
    <field>
      <a>srcField</a>
      <b>destField</b>
    </field>
  </mapping>
</mappings>

解决方法:

从Dozer的5.5.1版本开始,为API syntax cannot perform all mappings.映射中的元素只能使用XML完成.

如果您可以接受绕过< configuration>通过添加一些重复限制,那么下面的API映射应与您的XML映射相匹配:

BeanMappingBuilder mappingBuilder = new BeanMappingBuilder() {
    @Override
    protected void configure() {

        String dateFormat = "MM/dd/yyyy HH:mm";

        mapping(TestObject.class, TestObjectPrime.class,
                TypeMappingOptions.wildcard(true),
                TypeMappingOptions.dateFormat(dateFormat))
                .fields("dateString", "dateObject");

        mapping(SomeObject.class, SomeOtherObject.class,
                TypeMappingOptions.dateFormat(dateFormat))
                .fields("srcField", "destField");
    }
};

DozerBeanMapper apiBeanMapper = new DozerBeanMapper();
apiBeanMapper.addMapping(mappingBuilder);

如果您对更多细节感兴趣,我在PasteBin中添加了一个简单的ApiAndXmlMappingTest example.

标签:dozer,java
来源: https://codeday.me/bug/20191121/2052488.html