使用Orika Mapper进行映射时,类型为List <Map <String,String >>的字段的内容丢失

时间:2019-11-12 07:42:31

标签: java orika

映射时,类型List<Map<String, String>>的字段是否遇到相同的问题?我没有例外,但是我需要对映射器进行配置吗?

这种情况是我有此字段的对象

List<Map<String, String>> aField

aField字段的内容为:

[
{
"key1" : "val1",
"key2": "val2"
}
]

当对象映射到另一个对象时,此字段的内容丢失给新映射的对象,当我尝试打印时,列表的大小仍为1,但map的内容为空(如下):

[
{}
]

我将不胜感激。谢谢。

代码如下:

这是要映射的类:

public class SampleClass {
    private List<Map<String, String>> aField = new ArrayList<>();
    private String bField;

    public List<Map<String, String>> getaField() {
        return aField;
    }

    public void setaField(List<Map<String, String>> aField) {
        this.aField = aField;
    }

    public String getbField() {
        return bField;
    }

    public void setbField(String bField) {
        this.bField = bField;
    }
}

然后我有一个映射器(我也在使用spring框架):

@Component
public class TransactionMapper extends ConfigurableMapper {
}

控制器上的某处(出于测试目的):

@Autowired
    TransactionMapper mapper;

    System.out.println("---------------- SAMPLE CLASS ------------- ");
                    SampleClass s1 = new SampleClass();
                    List<Map<String, String>> aField = Lists.newArrayList();
                    Map<String, String> map = new HashMap<>();
                    map.put("key1", "val1");
                    map.put("key2", "val2");
                    aField.add(map);
                    s1.setaField(aField);
                    s1.setbField("Sample text");

                    System.out.println("****** s1 contents ******");
                    System.out.println("--- s1 aField: " + ", list size: " + s1.getaField().size());
                    s1.getaField().get(0).forEach((k, v) -> System.out.println((k + ":" + v)));
                    System.out.println("--- s1 bField content: " + s1.getbField());
                    System.out.println("****** end of s1 contents ******");

                    SampleClass s2 = new SampleClass();
                    mapper.map(s1, s2);
                    System.out.println("****** s2 contents ******");
                    System.out.println("--- s2 aField: " + ", list size: " + s2.getaField().size());
                    s2.getaField().get(0).forEach((k, v) -> System.out.println((k + ":" + v)));
                    System.out.println("--- s2 bField content: " + s2.getbField());
                    System.out.println("****** end of s2 contents ******");

                    System.out.println("---------------- SAMPLE CLASS ------------- ");

输出:

****** s1 contents ******
--- s1 aField: , list size: 1
key1:val1
key2:val2
--- s1 bField content: Sample text
****** end of s1 contents ******
****** s2 contents ******
--- s2 aField: , list size: 1
--- s2 bField content: Sample text
****** end of s2 contents ******
---------------- SAMPLE CLASS ------------- 

如您所见,s2的aField没有打印的内容。

1 个答案:

答案 0 :(得分:-1)

经过研究,我发现了以下解决方案:

  • 使用CustomMapper而不是ConfigurableMapper。
  • 不要忘记创建映射器工厂。

首先将其添加到TransactionMapper类:

public class TransactionMapper extends ConfigurableMapper{

    public TransactionMapper(MapperFactory f) {
        configure(f);
    } 

    public void configure(MapperFactory f) {
        f.classMap(SampleClass.class, SampleClass.class).customize(new CustomMapper<SampleClass, SampleClass>() {
            //Override mapping method and set source fields to destination fields
            @Override
            public void mapAtoB(SampleClass source, SampleClass destination, MappingContext context) {
                destination.setaField(source.getaField());
                destination.setbField(source.getbField());
            }
        }).byDefault().register();
    }
}

然后您的主类应该看起来像这样:

...
MapperFactory factory = new DefaultMapperFactory.Builder().build(); //Create the mapper factory
TransactionMapper mapper = new TransactionMapper(factory); //Call the mapper constructor

System.out.println("---------------- SAMPLE CLASS ------------- ");
SampleClass s1 = new SampleClass();
List<Map<String, String>> aField = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<>();
map.put("key1", "val1");
map.put("key2", "val2");
aField.add(map);
s1.setaField(aField);
s1.setbField("Sample text");

System.out.println("****** s1 contents ******");
System.out.println("--- s1 aField: " + ", list size: " + s1.getaField().size());
s1.getaField().get(0).forEach((k, v) -> System.out.println((k + ":" + v)));
System.out.println("--- s1 bField content: " + s1.getbField());
System.out.println("****** end of s1 contents ******");

SampleClass s2 = mapper.map(s1, SampleClass.class);
System.out.println("****** s2 contents ******");
System.out.println("--- s2 aField: " + ", list size: " + s2.getaField().size());
s2.getaField().get(0).forEach((k, v) -> System.out.println((k + ":" + v)));
System.out.println("--- s2 bField content: " + s2.getbField());
System.out.println("****** end of s2 contents ******");

System.out.println("---------------- SAMPLE CLASS ------------- ");
...

输出:

---------------- SAMPLE CLASS ------------- 
****** s1 contents ******
--- s1 aField: , list size: 1
key1:val1
key2:val2
--- s1 bField content: Sample text
****** end of s1 contents ******
****** s2 contents ******
--- s2 aField: , list size: 1
key1:val1
key2:val2
--- s2 bField content: Sample text
****** end of s2 contents ******
---------------- SAMPLE CLASS ------------- 

让我知道它是否有效。如果您有任何疑问,请回答,我会尽力回答。如果这样做有效,请将答案标记为解决方案,以便其他有相同问题的人知道该怎么办。 :)