提问人:user1345883 提问时间:6/2/2017 最后编辑:imTachuuser1345883 更新时间:6/13/2017 访问量:1294
Orika 列表中没有空元素的映射
Orika no mapping of null elements in list
问:
我有以下班级:
public class A{
List<AA> aaList;
public A(List<AA> aaList){
this.aaList = aaList;
}
//getters and setters + default constructor
public class AA {
String aaString;
public AA(String aaString){
this.aaString = aaString;
}
//getters and setters + default constructor
}
}
我想有两个同一类的对象,比方说:
A a = new A(Arrays.asList(new A.AA(null)));
A a2 = new A(Arrays.asList(new A.AA("test")));
当我映射到 时,应该保留,因为有一个 .a
a2
a2
test
a
null
我怎样才能配置它?Orika
我尝试了类似的东西:
mapperFactory.classMap(A.AA.class, A.AA.class)
.mapNulls(false)
.byDefault()
.register();
mapperFactory.classMap(A.class, A.class)
.mapNulls(false)
.customize(new CustomMapper<A, A>() {
@Override public void mapAtoB(A a, A a2,
MappingContext context) {
map(a.getAAList(), a2.getAAList());
}
})
.byDefault()
.register();
提前致谢
答:
0赞
Danylo Zatorsky
6/13/2017
#1
这是一个对我有用的修改后的代码片段:
mapperFactory.classMap(A.class, A.class)
.mapNulls(false)
.customize(new CustomMapper<A, A>() {
@Override
public void mapAtoB(A a, A a2, MappingContext context) {
// 1. Returns new list with not null
List<A.AA> a1List = a.getAaList().stream()
.filter(a1 -> a1.getAaString() != null)
.collect(Collectors.toList());
// 2. Merges all the elements from 'a2' list into 'a' list
a1List.addAll(a2.getAaList());
// 3. Sets the list with merged elements into the 'a2'
a2.setAaList(a1List);
}
})
.register();
请注意,应删除 ,以便自定义映射器正常工作。.byDefault()
评论
a
a2