如何使用 Dozer 将字符串值映射到字符串列表?

How to map String values to a String list using Dozer?

您好,我尝试将以下源 class 映射到以下目标 class。为了将字符串值映射到列表字符串中,我使用了以下映射。它没有正确映射。我需要知道如何使用 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> 

为此,您需要使用 custom converter

文档会给你一个更透彻的理解,但本质上,目前 dozer 不知道如何将字符串转换为列表,所以你必须告诉它。

您的自定义转换器会将字符串值作为源并将列表作为目标并将其接收到的字符串添加到列表中。

大致如下:

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> 

您需要在 Java 中创建自定义转换器 class。

参考 Dozer Custom Converters,转到标题新建自定义转换器 API。

您的转换器 class 需要像这样扩展 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>

只需通过 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>

不需要自定义转换器。

这是我想出的答案..

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>