如何解决ModelMapper 自定义 PropertyMap
我想使用模型映射器将我的实体转换为 DTO 对象,但是如果可能的话,我想在映射器内部实现一些自定义逻辑。目前我正在努力解决空指针异常 在控制器内部,我将自定义属性映射和实体映射列表添加到 DTO
ModelMapper modelMapper = new ModelMapper();
modelMapper.addMappings(new UserPropertyMap());
final List<UserDTO> userDTOList = modelMapper.map(userRepository.findAll(),USERDTO_LIST_TYPE);
我的房产地图看起来如何
public class UserPropertyMap extends PropertyMap<User,UserDTO> {
@Override
protected void configure() {
map().setDocumentsCount(source.getDocuments().size());
}
}
例外:
2021-03-12 09:33:54.086 ERROR 19876 --- [nio-9724-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.modelmapper.ConfigurationException: ModelMapper configuration errors:
1) Failed to configure mappings
1 error] with root cause
java.lang.NullPointerException: null
at org.modelmapper.internal.ExplicitMappingBuilder$ExplicitMappingInterceptor.access$000(ExplicitMappingBuilder.java:304) ~[modelmapper-2.3.9.jar:na]
at org.modelmapper.internal.ExplicitMappingBuilder.createAccessorProxies(ExplicitMappingBuilder.java:287) ~[modelmapper-2.3.9.jar:na]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。