我想使用 Orika 库映射具有嵌套集合的字段。我在课堂上的领域定义为:
private final List<List<Pojo>> list = new LinkedList<List<Pojo>>();
Pojo 是一个简单的 POJO 类。不幸的是,我在 Orika 的内部逻辑中遇到了由 NullPointerException 引起的 MappingException。
我做错了什么吗?也许我需要使用自定义映射功能?
编辑:
这是我的代码:
public class Pojo {
private int field;
public int getField() {
return field;
}
public void setField(final int field) {
this.field = field;
}
}
public class Source { private final List> list = new LinkedList>();
public List<List<Pojo>> getList() {
return list;
}
}
public class Destination { private final List> listDest = new LinkedList>();
public List<List<Pojo>> getListDest() {
return listDest;
}
}
公共类主要{
public static void main(final String[] args) {
final MapperFactory factory = new DefaultMapperFactory.Builder().build();
factory.classMap(Source.class, Destination.class).field("list", "listDest").byDefault().register();
final Source src = new Source();
final LinkedList<Pojo> nestedList = new LinkedList<Pojo>();
final Pojo pojo = new Pojo();
pojo.setField(8978);
nestedList.add(pojo);
src.getList().add(nestedList);
final MapperFacade facade = factory.getMapperFacade();
final Destination dest = facade.map(src, Destination.class);
System.out.println(dest.getListDest().get(0).get(0).getField());
}
}
执行上述代码会导致此异常:
Exception in thread "main" ma.glasnost.orika.MappingException: Error encountered while mapping for the following inputs:
rawSource=com.bbh.nested.Source@39185ce6
sourceClass=class com.bbh.nested.Source
destinationClass=class com.bbh.nested.Destination