我尝试将 DemoEntity 转换为 DemoDto 。他们有子属性Collection<SubEntity>
和Collection<SubDto>
。转换Collection<SubEntity>
时发生错误。如果我不添加@JMap("subEntities")
,JMapper将运行良好。现在我希望在转换DemoEntity
时,Collection<SubEntity>
可以一起转换。
public class DemoEntity {
/** The id. */
private int id;
/** The code. */
private String code;
/** The date. */
private Date date;
private Collection<SubEntity> subEntities;
@JMapConversion(from = "date", to = "date")
public String convertDate(Date date) {
return date.toString();
}
public DemoEntity(int id, String code, Date date, Collection<SubEntity> subEntities) {
super();
this.id = id;
this.code = code;
this.date = date;
this.subEntities = subEntities;
}
...setter and getter...
}
public class DemoDto {
/** The id. */
@JMap
private Long id;
/** The code. */
@JMap
private String code;
/** The date. */
@JMap
private String date;
@JMap("subEntities")
private Collection<SubDto> subDtos;
... setter and getter...
}
主要方法如下:
Collection<SubEntity> subEntities = new ArrayList<>();
for (int i = 1; i < 100; i++) {
SubEntity subEntity = new SubEntity(i, "C" + i, new Date());
subEntities.add(subEntity);
}
DemoEntity demoEntity = new DemoEntity(1, "D" + 1, new Date(), subEntities);
JMapperAPI api = new JMapperAPI();
MappedClass mappedClass = new MappedClass(SubDto.class);
api.add(mappedClass);
JMapper<DemoDto, DemoEntity> mapper = new JMapper<>(DemoDto.class, DemoEntity.class, api);
DemoDto demoDto = mapper.getDestination(demoEntity);
System.out.println(demoDto);
Exception in thread "main" com.googlecode.jmapper.exceptions.JMapperException: java.lang.NullPointerException
at com.googlecode.jmapper.config.JmapperLog.ERROR(JmapperLog.java:46)
at com.googlecode.jmapper.JMapper.<init>(JMapper.java:445)
at com.googlecode.jmapper.JMapper.<init>(JMapper.java:411)
at com.googlecode.jmapper.JMapper.<init>(JMapper.java:385)
at com.jmapper.JmapperDemo.main(JmapperDemo.java:35)
Caused by: java.lang.NullPointerException
at com.googlecode.jmapper.operations.complex.AComplexOperation.newInstance(AComplexOperation.java:107)
at com.googlecode.jmapper.operations.complex.AComplexOperation.newInstance(AComplexOperation.java:98)
at com.googlecode.jmapper.operations.recursive.MappedCollectionOperation.sharedCode(MappedCollectionOperation.java:64)
at com.googlecode.jmapper.operations.complex.AComplexOperation.sourceControl(AComplexOperation.java:156)
at com.googlecode.jmapper.operations.complex.AComplexOperation.genericFlow(AComplexOperation.java:133)
at com.googlecode.jmapper.operations.complex.AComplexOperation.write(AComplexOperation.java:89)
at com.googlecode.jmapper.generation.MapperConstructor.mapping(MapperConstructor.java:154)
at com.googlecode.jmapper.generation.MapperConstructor.wrappedMapping(MapperConstructor.java:123)
at com.googlecode.jmapper.generation.MapperConstructor.getMappings(MapperConstructor.java:89)
at com.googlecode.jmapper.generation.MapperGenerator.generateMapperClass(MapperGenerator.java:54)
at com.googlecode.jmapper.generation.MapperBuilder.generate(MapperBuilder.java:88)
at com.googlecode.jmapper.JMapper.createMapper(JMapper.java:458)
at com.googlecode.jmapper.JMapper.<init>(JMapper.java:440)
... 3 more
此外,是否有任何方法将Collection<Entity>
转换为Collection<Dto>
答案 0 :(得分:0)
这是递归映射的一种情况,需要映射子类。 当您使用DemoEntity映射DemoDTO时,您需要使用SubEntity映射SubDTO。