简单映射配置List <object>字段

时间:2015-10-08 11:59:08

标签: java orika

我尝试使用几乎相同的字段映射两个类:MetaInfoDTOMetaInfo

我尝试使用默认映射配置映射器。问题出在List<Object> values字段上。它在另一个类上具有相同的名称。所以,我想它必须正确映射。最后参见测试。

public class MetaInfoDTO
{

    private String id;
    private String key;
    private String description;
    private List<Object> values;

    //Constructors, getters and setters...
    public List<Object> getValues() {
        return values;
    }

    public void setValues(List<Object> values) {
        this.values = values;
    }
}

public class MetaInfo
{

    private String id;
    private String key;
    private String description;
    private List<Object> values;

    //Constructors, getters and setters...
    public List<Object> getValues() {
        return values;
    }

    public void setValues(List<Object> values) {
        this.values = values;
    }
}

这个orika配置:

mapperFactory.classMap(MetaInfoDTO.class, MetaInfo.class)
        .byDefault()
        .register();

我已经创建了一个测试以显示我的问题:

@Test
public void metaInfoDTOTOMetaInfoWITHVALUES()
{
    MetaInfoDTO dto = new StringMetaInfoDTO("key1", "desc1");
    MetaInfo metainfo = this.mappingResources.getMapper().map(dto, MetaInfo.class);
    for (int i = 0; i < 10; i++)
        dto.getValues().add("s" + i);

    assertThat(dto.getValues(), not(empty()));
    assertThat(metainfo.getValues(), not(empty()));   //<<<< IT'S EMPTY!!
}

类别:

public class StringMetaInfoDTO extends MetaInfoDTO
{

    public StringMetaInfoDTO(String key, String description) {
        this(key, description, new ArrayList<String>());
    }

    public StringMetaInfoDTO(String key, String description, List<String> values) {
        super(MetaInfoTypeDTO.stringType, key, description, values.stream().map(e -> (Object)e).collect(Collectors.toList()));
    }

}

因此,在映射后,metainfo.getValues()为空! 有什么不对吗?

0 个答案:

没有答案