0
我想在源和目標對象中具有相同名稱但不同類型的字段之間使用Dozer
進行自定義類型轉換。 我編寫的代碼片段,解釋我的意圖:推土機5.4 setCustomConverter不能在相同名稱但不同類型的字段之間工作
package com;
import org.dozer.CustomConverter;
import org.dozer.DozerBeanMapper;
import org.dozer.DozerConverter;
import java.util.Arrays;
public class DozerTest
{
public static void main(String[] args)
{
DozerBeanMapper mapper = new DozerBeanMapper();
mapper.setCustomConverters(Arrays.<CustomConverter>asList(new DozerConverter<Version, String>(Version.class, String.class)
{
@Override public String convertTo(Version version, String versionOrig)
{
return version.getFullVersion();
}
@Override public Version convertFrom(String version, Version versionOrig)
{
return new Version(version);
}
}));
OldPackage oldPackage = new OldPackage();
oldPackage.setVersion("1.1");
NewPackage newPackage = mapper.map(oldPackage, NewPackage.class);
System.out.println(newPackage.getVersion().getFullVersion());
}
public static class NewPackage
{
private Version version;
public Version getVersion()
{
return version;
}
public void setVersion(Version version)
{
this.version = version;
}
}
public static class OldPackage
{
private String version;
public String getVersion()
{
return version;
}
public void setVersion(String version)
{
this.version = version;
}
}
private static class Version
{
private final String fullVersion;
private Version(String fullVersion)
{
this.fullVersion = fullVersion;
}
public String getFullVersion()
{
return fullVersion;
}
}
}
我收到此錯誤信息:
Exception in thread "main" org.dozer.MappingException: Illegal object type for the method 'setVersion'.
Expected types:
com.DozerTest$Version
Actual types:
java.lang.String
任何人有一個想法,我做錯了什麼?