您好我尝试将以下Source类映射到以下Destination类。我使用以下映射来将字符串值映射到列表字符串。它没有正确映射。我需要知道如何使用Dozer将2个字符串值映射到一个目标字符串列表中。
public class SourceClass {
protected String streetName;
protected String additionalStreetName;
}
public class Destination {
protected List<String> addressLine;
}
<mapping map-id="newId" >
<class-a>myPackage.SourceClass </class-a>
<class-b>myPackage.Destination</class-b>
<field>
<a>streetName</a>
<b>addressLine[0]</b>
</field>
<field>
<a>additionalStreetName</a>
<b>addressLine[1]</b>
</field>
</mapping>
答案 0 :(得分:2)
要执行此操作,您需要使用custom converter。
该文档将为您提供更全面的理解,但实质上,目前推土机不知道如何将字符串转换为列表,因此您必须告诉它。
您的自定义转换器会将String值作为源并将List作为目标,并将其收到的字符串添加到列表中。
有些事情如下:
public class TestCustomConverter extends DozerConverter {
public NewDozerConverter() {
super(String.class, List.class);
}
public List<String> convertTo(String source, List<String> destination) {
if (source == null) {
return new ArrayList<>();
}
if (destination == null) {
destination = new ArrayList<>();
}
destination.add(source);
return destination;
}
public String convertFrom(List<String> source, String destination {
return null;
}
}
您的映射将如下所示:
<mapping map-id="newId" >
<class-a>myPackage.SourceClass </class-a>
<class-b>myPackage.Destination</class-b>
<field custom-converter="TestCustomConverter">
<a>streetName</a>
<b>addressLine</b>
</field>
<field custom-converter="TestCustomConverter">
<a>additionalStreetName</a>
<b>addressLine</b>
</field>
</mapping>
答案 1 :(得分:1)
您需要在Java
中创建自定义转换器类。
参考Dozer Custom Converters,转到标题New Custom Converter API。
你的转换器类需要扩展DozerConverter
,如下所示:
SourceClassToDestinationConverter extends DozerConverter <SourceClass, Destination> implements MapperAware
然后,您必须在custom-converters
配置中定义要用于代替映射器的转换器,如下所示:
<configuration>
<custom-converters>
<converter
type="some.package.converter.SourceClassToDestinationConverter ">
<class-a>myPackage.SourceClass</class-a>
<class-b>myPackage.Destination</class-b>
</converter>
...
</custom-converters>
</configuration>
答案 2 :(得分:1)
只需按Hint
标记指定目标列表中的对象类型,即可让Dozer
知道您希望在目标列表中创建的对象类型:
<field>
<a>streetName</a>
<b>addressLine[0]</b>
<b-hint>java.lang.String</b-hint>
</field>
<field>
<a>additionalStreetName</a>
<b>addressLine[1]</b>
<b-hint>java.lang.String</b-hint>
</field>
无需自定义转换器。
答案 3 :(得分:1)
这是我想出的答案..
import java.util.ArrayList;
import java.util.List;
import org.dozer.DozerConverter;
import org.dozer.Mapper;
import org.dozer.MapperAware;
public class TestConverter extends DozerConverter<String, List>
implements MapperAware {
public TestConverter() {
super(String.class, List.class);
// TODO Auto-generated constructor stub
}
@Override
public void setMapper(Mapper mapper) {
// TODO Auto-generated method stub
}
@Override
public List convertTo(String source, List destination) {
if (source == null) {
return new ArrayList<String>();
}
if (destination == null) {
destination = new ArrayList<String>();
}
//destination.getAddressLine().add(source.getAdditionalStreetName());
destination.add(source);
return destination;
}
@Override
public String convertFrom(List source, String destination) {
// TODO Auto-generated method stub
return null;
}
}
===============在配置下的dozerMapper.xml中========================== =
<custom-converters>
<converter
type="company.realMap.TestConverter">
<class-a>desAdv.SourceClass</class-a>
<class-b>abcReq.Destination</class-b>
</converter>
===============在dozerMapper.xml下的映射==========================
<field custom-converter="myPackage.TestConverter">
<a>streetName</a>
<b>addressLine</b>
</field>
<field custom-converter="myPackage.TestConverter">
<a>additionalStreetName</a>
<b>addressLine</b>