21

I have a simple class that I want to map to a DTO class using modelMapper.

class Source {

    private String name;
    private String address;
    List<Thing> things;

    // getters and setters follows
    }

    class Thing {

    private String thingCode;
    private String thingDescription;

    // getters and setters
}

and I want to convert these to a sourceDTO that contains a list of ThingDTOs, for example

class sourceDTO {

    private String name;
    private String address;
    List<ThingDTO> things;

    // getters and setters.
    }

     class ThingDTO {

    private String thingCode;
    private String thingDescription;

    // getters and setters
}

If I drop my list of Things and list of ThingsDTO then modelmapper is a delight to use,

 modelMapper.map(source, SourceDTO.class);

But I can't work out how to get the mapper to convert the List of Things to List of ThingDTOs. From the documentation, I think I need to create a mapper class that extends PropertyMap but I can't work out how to configure it.

Any pointers to the relevant documentation would be welcome

user497087
  • 1,561
  • 3
  • 24
  • 41

2 Answers2

20

I think if you configure your ModelMapper as LOOSE or STANDARD it will do for you.

modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.LOOSE);

Otherwhise you could try next:

  1. You may create a converter like:

    public class ListThingToThingDTOConverter implements Converter<List<Thing>, List<ThingDTO>> {
    
    
    @Override
    public List<ThingDTO> convert(MappingContext<List<Thing>, List<ThingDTO>> context) {
        List<Thing> source = context.getSource();
        List<ThingDTO> output = new ArrayList<>();
        ...
        //Convert programmatically List<Thing> to List<ThingDTO>
        ...
    
        return output;
      }}
    
  2. Then customize a Mapping Thing to ThingDTO as next:

        public class SourceToSourceDTOMap extends PropertyMap<Thing, ThingDTO> {
              @Override
              protected void configure(){
                   using(new ListThingToThingDTOConverter()).map(source.getThings()).setThings(null);
              }
    
  3. Finally you must add SourceToSourceDTOMap to your ModelMapper as below:

    modelMapper = new ModelMapper();
    modelMapper.addMappings(new SourceToSourceDTOMap());
    
Pau
  • 14,917
  • 14
  • 67
  • 94
  • 1
    I have a slightly different use-case. Would you mind taking a look at it? https://stackoverflow.com/q/69215180/9768291 – payne Sep 17 '21 at 13:41
  • 2
    Thank you for this. I dont know why this is not very well explain in many places.. This is like the basic requirement and had to search around a lot to find the solution for this. Appreciate your answer. Save a lot of time. – codeMan Mar 26 '22 at 17:25
2

You can map like the below code by creating generics . link for reference

http://modelmapper.org/user-manual/generics/

imports :

import java.lang.reflect.Type;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;

In your service or controller class:

ModelMapper modelMapper = new ModelMapper();
Type listType = new TypeToken<SourceDTO>(){}.getType();
SourceDTO sourceDTO = modelMapper.map(source,listType);
Vignesh_A
  • 508
  • 1
  • 7
  • 19