簡體   English   中英

使用推土機將基本類(字符串,布爾值等)相互映射

[英]Mapping primitive classes (String, Boolean, etc) to each other with Dozer

我正在嘗試使用推土機自動從原始類相互映射。 最后,代碼可能最終看起來像這樣。

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

當在Dozer中,Dozer確實支持將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來將Boolean轉換為String,但是我不想重新實現Dozer已經擁有的代碼。 有什么方法可以使Dozer直接在原始類型之間進行映射?

您可以使用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);

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM