考虑下面的例子。
Group.java(GroupDTO.java也具有相同的属性)
import java.util.List;
public class Group {
public Group(List<Person> persons) {
this.persons = persons;
}
private List<Person> persons;
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
}
Person.java(PersonDTO.java也具有相同的属性)
import java.util.List;
public class Person {
public Person(){}
public Person(List<Name> names, Integer age) {
this.names = names;
this.age = age;
}
private List<Name> names;
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Name> getNames() {
return names;
}
public void setNames(List<Name> names) {
this.names = names;
}
}
Name.java(NameDTO.java也具有相同的属性)
public class Name {
public Name(String first, String last) {
this.first = first;
this.last = last;
}
private String first;
private String last;
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
}
当我运行以下课程时,我得到以下异常。
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.ClassMapBuilder;
import java.util.LinkedList;
import java.util.List;
public class Mapper {
public static void main(String[] args) {
List<Name> names = new LinkedList<Name>();
names.add(new Name("foo", "bar"));
names.add(new Name("lorem", "ipsum"));
List<Person> persons = new LinkedList<Person>();
persons.add(new Person(names, 30));
persons.add(new Person(names, 40));
Group g1 = new Group(persons);
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
ClassMapBuilder classMapBuilder = mapperFactory.classMap(Group.class, GroupDTO.class);
classMapBuilder.field("persons{age}", "persons{age}");
classMapBuilder.field("persons{names{first}}", "persons{names{first}}");
classMapBuilder.field("persons{names{last}}", "persons{names{last}}");
classMapBuilder.byDefault().register();
MapperFacade mapper = mapperFactory.getMapperFacade();
GroupDTO groupDTO = mapper.map(g1, GroupDTO.class);
for(Person p:groupDTO.getPersons()){
System.out.println(p.getAge());
for(Name n:p.getNames()){
System.out.println(n.getFirst());
System.out.println(n.getFirst());
}
}
}
}
异常
Exception in thread "main" ma.glasnost.orika.MappingException: java.lang.NullPointerException
at ma.glasnost.orika.impl.generator.MapperGenerator.build(MapperGenerator.java:104)
at ma.glasnost.orika.impl.DefaultMapperFactory.buildMapper(DefaultMapperFactory.java:1480)
at ma.glasnost.orika.impl.DefaultMapperFactory.build(DefaultMapperFactory.java:1295)
at ma.glasnost.orika.impl.DefaultMapperFactory.getMapperFacade(DefaultMapperFactory.java:883)
at Mapper.main(Mapper.java:28)
Caused by: java.lang.NullPointerException
at ma.glasnost.orika.impl.generator.specification.MultiOccurrenceToMultiOccurrence.registerClassMaps(MultiOccurrenceToMultiOccurrence.java:397)
at ma.glasnost.orika.impl.generator.specification.MultiOccurrenceToMultiOccurrence.generateMappingCode(MultiOccurrenceToMultiOccurrence.java:72)
at ma.glasnost.orika.impl.generator.SourceCodeContext.mapAggregateFields(SourceCodeContext.java:721)
at ma.glasnost.orika.impl.generator.MapperGenerator.addMapMethod(MapperGenerator.java:182)
at ma.glasnost.orika.impl.generator.MapperGenerator.build(MapperGenerator.java:70)
... 4 more
很抱歉这篇长篇文章。我正在尝试List&lt;&gt;内的地图字段这又在另一个List&lt;&gt;内。什么是最好的方法或我在这里做错了什么?
答案 0 :(得分:0)
如果双方的字段名称相同,则您不必注册字段映射,Orika将自行处理。此代码将正确执行映射:
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
ClassMapBuilder classMapBuilder = mapperFactory.classMap(Group.class, GroupDTO.class);
classMapBuilder.byDefault().register();
MapperFacade mapper = mapperFactory.getMapperFacade();
GroupDTO groupDTO = mapper.map(g1, GroupDTO.class);
此外,可能只是一个拼写错误,但getPersons()
的{{1}}应该返回GroupDTO
,PersonDTO
和getNames()
也是如此:
NameDTO