ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – ModelMapper转换器 – 不工作

2019-06-08 18:05:41  阅读:314  来源: 互联网

标签:java spring modelmapper


我在我的其他应用程序中使用ModelMapper.

我必须将List转换为List.

这是我的代码:

 Converter<List<UserRole>,List<String>> listConverter = new Converter<List<UserRole>, List<String>>() {
    public List<String> convert(MappingContext<List<UserRole>, List<String>> context) {
        List<String> target = new ArrayList<String>();
        List<UserRole> userRoles = context.getSource();
        for (UserRole userRole : userRoles) {
            target.add(userRole.getRole().getName());
        }
        return target;
    }
};

PropertyMap<User, UserDTO> propertiesForConvertToDto = new PropertyMap<User, UserDTO>() {
    protected void configure() {
        using(listConverter).map(source.getUserRoles()).setRoles(null);
    }
};

当我运行应用程序时,我收到此错误:

    HTTP Status 500 - Request processing failed; nested exception is org.modelmapper.MappingException: ModelMapper mapping errors:

type Exception report

message Request processing failed; nested exception is org.modelmapper.MappingException: ModelMapper mapping errors:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.modelmapper.MappingException: ModelMapper mapping errors:

1) Failed to instantiate instance of destination java.util.List. Ensure that java.util.List has a non-private no-argument constructor.
Caused by: java.lang.NoSuchMethodException: java.util.List.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getDeclaredConstructor(Class.java:2178)
    at org.modelmapper.internal.MappingEngineImpl.instantiate(MappingEngineImpl.java:366)
    at org.modelmapper.internal.MappingEngineImpl.createDestination(MappingEngineImpl.java:382)

你能帮助我吗?我正在尝试解决这个问题五个小时.当我调试时,我知道转换器正常工作.我可以不正确地称为转换器吗?

解决方法:

在映射之前,请使用以下代码片段进行ModelMapper实现.

import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Component;

    @Component
    public class ModelMapperUtil extends ModelMapper{
        public ModelMapperUtil() {       
        this.getConfiguration().setFieldMatchingEnabled(true).setFieldAccessLevel(org.modelmapper.config.Configuration.AccessLevel.PRIVATE);
        }   
    }

现在尝试映射对象值,如

//implicit maaping    
UserDTO dto = mapper.map(userVO, UserDTO.class);

别忘了添加jar文件
https://mvnrepository.com/artifact/org.modelmapper/modelmapper/0.7.5

标签:java,spring,modelmapper
来源: https://codeday.me/bug/20190608/1199392.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有