【问题标题】:ModelMapper converter - not workingModelMapper 转换器 - 不工作
【发布时间】:2016-08-17 17:13:57
【问题描述】:

我在其他应用程序中使用 ModelMapper。

我必须将列表转换为列表。

这是我的代码:

 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 mappermapper.addMappings(propertiesForConvertToDto)?。请添加您的ModelMapperconfiguration(重要)和实体UserUserDto(如果可能的话)。

标签: java spring modelmapper


【解决方案1】:

请在映射前使用下面的 sn-p 进行 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

【讨论】:

    【解决方案2】:

    请确保您已定义您的 ModelMapper bean

    //Define ModelMapper class in your configuration
        @Bean
        public ModelMapper modelMapper(){
            ModelMapper modelMapper = new ModelMapper();
            modelMapper.addMappings(propertiesForConvertToDto);
            return modelMapper;
        }
    

    【讨论】:

      【解决方案3】:

      您尝试映射到目标类型 List。尝试列出一个具体的列表实现,例如数组列表。如您所见,问题在于模型映射器无法实例化列表。如果您使用具体类型,应该可以解决。 另一种尝试是,您使用实例化适当类型的提供程序。 亲切的问候。

      【讨论】:

        猜你喜欢
        • 2021-08-28
        • 1970-01-01
        • 1970-01-01
        • 2018-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多