如何将列表映射到其大小

时间:2019-03-18 20:42:05

标签: java modelmapper

在我的实体中,我有一个带有书架的对象架子

public class Shelf{
  ...
  private List<Book> books;
}

在我的ShelfDTO中,我只需要有书的数量

public class ShelfDTO{
  ...
  private int booksNumber;
}

我尝试过:

modelMapper.addMappings(new PropertyMap<Shelf, ShelfDTO>() {
        protected void configure() {
            map().setBooksNumber(source.getBooks().size());
        }
    });

但是尝试启动应用程序时出现了异常:

Caused by: java.lang.NullPointerException: null
    at org.modelmapper.internal.ExplicitMappingBuilder$ExplicitMappingInterceptor.access$000(ExplicitMappingBuilder.java:304) ~[modelmapper-2.3.0.jar:na]
    at org.modelmapper.internal.ExplicitMappingBuilder.createAccessorProxies(ExplicitMappingBuilder.java:287) ~[modelmapper-2.3.0.jar:na]
    at org.modelmapper.internal.ExplicitMappingBuilder.createProxies(ExplicitMappingBuilder.java:277) ~[modelmapper-2.3.0.jar:na]

是否可以通过属性映射来做到这一点,还是必须使用Converter?

0 个答案:

没有答案