Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use modelMapper to convert nested classes

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

like image 999
user497087 Avatar asked Apr 19 '16 11:04

user497087


People also ask

How does ModelMapper work in Java?

ModelMapper consists of two separate processes: the matching process, where a source and destination type's properties are matched to each other, and the mapping process where matched property values are converted from a source to destination object. A further description of these processes follows.

How do you use a model mapper converter?

The first is by adding the converter to a ModelMapper: modelMapper. addConverter(personConverter); This, in turn, sets the converter against the TypeMap corresponding to the source and destination types Person and PersonDTO .

What is spring boot ModelMapper?

The goal of ModelMapper is to make object mapping easy by automatically determining how one object model maps to another.

How do you customize Model Mapper?

Each TypeMap contains a PropertyMap with a list of mappings. So in the example the mm will automatically create a TypeMap<DogData, DogInfo> that contains a PropertyMap that has a single mapping. do one of the following: if the TypeMap has a custom Converter, call it.


2 Answers

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());
    
like image 127
Pau Avatar answered Oct 09 '22 04:10

Pau


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);
like image 35
Vignesh_A Avatar answered Oct 09 '22 03:10

Vignesh_A