ModelMapper converter - not working
Asked Answered
F

3

8

I'm using ModelMapper in my rest apps.

I have to convert List to List.

This is my code:

 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);
    }
};

When I'm running app I get this error:

    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)

Can You help me? I'm trying solve this problem for five hours. When I'm debugging I knew that converter work correctly. May don't I correctly called converter?

Fuentes answered 17/8, 2016 at 17:13 Comment(1)
Have you added the mapping to your ModelMapper mapper as mapper.addMappings(propertiesForConvertToDto)?. Please add your ModelMapperconfiguration (important) and the entities Userand UserDto if is possible with the properties as well (if you want).Nottingham
L
3

Please use below snippet for ModelMapper implementation before mapping.

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);
        }   
    }

Now try to map the objects values like

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

Dont forget to add jar file https://mvnrepository.com/artifact/org.modelmapper/modelmapper/0.7.5

Louie answered 30/9, 2018 at 16:11 Comment(0)
A
2

Please make sure you have defined your ModelMapper bean

//Define ModelMapper class in your configuration
    @Bean
    public ModelMapper modelMapper(){
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.addMappings(propertiesForConvertToDto);
        return modelMapper;
    }
Apia answered 9/3, 2017 at 7:7 Comment(0)
E
0

you try to map to the destination type List. Try instead list a concrete list implementation, e.g. ArrayList. As you can see, the problem is, that model mapper cannot instantiate the list. Should be solved if you use a concrete type. Another try can be, you use a provider which instantiates the appropriate type. Kind regards.

Etiolate answered 22/7, 2019 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.