use*_*883 6 java null mapper orika
我有以下课程:
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
}
}
Run Code Online (Sandbox Code Playgroud)
我希望有两个同类的对象,让我们说:
A a = new A(Arrays.asList(new A.AA(null)));
A a2 = new A(Arrays.asList(new A.AA("test")));
Run Code Online (Sandbox Code Playgroud)
当我映射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();
Run Code Online (Sandbox Code Playgroud)
提前致谢
这是一个对我有用的修改后的代码片段:
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();
Run Code Online (Sandbox Code Playgroud)
请注意,.byDefault()应该删除 以使自定义映射器正常工作。
| 归档时间: |
|
| 查看次数: |
537 次 |
| 最近记录: |