【问题标题】:Mapping primitive classes (String, Boolean, etc) to each other with Dozer使用 Dozer 将原始类(字符串、布尔值等)相互映射
【发布时间】:2011-03-08 17:23:36
【问题描述】:

我正在尝试使用 Dozer 自动从原始类相互映射。最后,代码可能会变成这样。

Boolean resultBoolean = mapper.map("true", Boolean.class);

虽然 Dozer 在 bean 中确实支持将 String 映射到 Boolean,但似乎直接映射到 Boolean 会产生以下异常。

org.dozer.MappingException: java.lang.NoSuchMethodException: java.lang.Boolean.<init>()
at org.dozer.util.MappingUtils.throwMappingException(MappingUtils.java:88)
at org.dozer.factory.ConstructionStrategies$ByConstructor.newInstance(ConstructionStrategies.java:261)
at org.dozer.factory.ConstructionStrategies$ByConstructor.create(ConstructionStrategies.java:245)
at org.dozer.factory.DestBeanCreator.create(DestBeanCreator.java:65)
at org.dozer.MappingProcessor.map(MappingProcessor.java:178)
at org.dozer.MappingProcessor.map(MappingProcessor.java:125)
at org.dozer.MappingProcessor.map(MappingProcessor.java:120)
at org.dozer.DozerBeanMapper.map(DozerBeanMapper.java:111)

...

Caused by: java.lang.NoSuchMethodException: java.lang.Boolean.<init>()
at java.lang.Class.getConstructor0(Class.java:2706)
at java.lang.Class.getDeclaredConstructor(Class.java:1985)
at org.dozer.factory.ConstructionStrategies$ByConstructor.newInstance(ConstructionStrategies.java:257)
... 32 more

很明显,Dozer 正在尝试实例化布尔值本身。我可以创建一个客户DozerConverter 将布尔值转换为字符串,但我不想重新实现 Dozer 已有的代码。有没有办法让 Dozer 直接映射到原始类型?

【问题讨论】:

    标签: java automapping dozer


    【解决方案1】:

    您可以使用 org.dozer.converters.PrimitiveOrWrapperConverter 代替 org.dozer.DozerBeanMapper

    import org.dozer.converters.DateFormatContainer;
    import org.dozer.converters.PrimitiveOrWrapperConverter;
    
    public class DozerPrimitiveMapping {
    
    
        public static void main(String[] args) {
    
            PrimitiveOrWrapperConverter primitiveConverter = new PrimitiveOrWrapperConverter();
            //DateFormatContainer is not needed in this String-to-Boolean use case, but the converter would throw an error if it was null
            DateFormatContainer dateFormatContainer = new DateFormatContainer("");
            Boolean booleanResult= (Boolean) primitiveConverter.convert("true", Boolean.class, dateFormatContainer);
            System.out.println("Boolean result from dozer: "+booleanResult);
        }
    }
    

    或者将其全部封装在自定义转换器中:

    package my.dozer.test;
    
    import org.dozer.CustomConverter;
    import org.dozer.converters.DateFormatContainer;
    import org.dozer.converters.PrimitiveOrWrapperConverter;
    
    public class DozerPrimitiveConverter implements CustomConverter {
    
        private final PrimitiveOrWrapperConverter primitiveConverter = new PrimitiveOrWrapperConverter();
        //DateFormatContainer is not needed in this String-to-Boolean use case, but the converter would throw an error if it was null
        private final DateFormatContainer dateFormatContainer = new DateFormatContainer("");
    
        @Override
        public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
            Boolean booleanResult = (Boolean) primitiveConverter.convert(sourceFieldValue, Boolean.class, dateFormatContainer);
            return booleanResult;
        }
    
    }
    

    并像在此示例配置文件dozer-primitive-mapping.xml 中一样配置转换器:

    <?xml version="1.0" encoding="UTF-8"?>
    <mappings xmlns="http://dozer.sourceforge.net"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://dozer.sourceforge.net
              http://dozer.sourceforge.net/schema/beanmapping.xsd">
    
        <configuration>
            <custom-converters>
              <converter type="my.dozer.test.DozerPrimitiveConverter" >
                  <class-a>java.lang.String</class-a>
                  <class-b>java.lang.Boolean</class-b>
              </converter>
            </custom-converters>
        </configuration>
    
    </mappings>
    

    使用自定义转换器运行映射的示例类:

    package my.dozer.test;
    
    import java.io.InputStream;
    import org.dozer.DozerBeanMapper;
    
    public class DozerPrimitiveConverterApp {
    
        public static void main(String[] args) {
            DozerBeanMapper mapper = new DozerBeanMapper();
            InputStream is = DozerPrimitiveConverterApp.class.getClassLoader().getResourceAsStream("dozer-primitive-mapping.xml");
            mapper.addMapping(is);
    
            Boolean booleanValue = mapper.map("false", Boolean.class);
            System.out.println("Boolean result from dozer with custom converter: " + booleanValue);
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多