3

I am using ModelMApper to map objects from DTO to Impl - when deserializing the object. This is done in combination with JAxRS. When user makes a POST/PUT request I dont want the "id" to ever be mapped.

I need to skip the "id" field for all the mappings. I dont want to do this one by one since there aren't necessarily explicit maps for all classes. How could I configure the mapper to skip all "id" fields from all DTOs to Impls mappings.

Thank you

allegjdm93
  • 592
  • 6
  • 24

2 Answers2

8

One approach is to use Conditions to conditionally map (or not) id properties. Something like this (not tested):

Condition skipIds = new Condition() {
    public boolean applies(MappingContext<Object, Object> context) {
        return !context.getMapping().getLastDestinationProperty().getName().equals("id");
    }
};

modelMapper.getConfiguration().setPropertyCondition(skipIds);

This sets the skipIds condition to be used globally for all properties, so that the mapping of values to any destination property with the name "id" will be skipped.

Jonathan
  • 5,027
  • 39
  • 48
3

@Jonathan 's solution worked for me with a slight modification. I had to change

public boolean applies(MappingContext<S, D> context)

to

public boolean applies(MappingContext context)
Rosa
  • 84
  • 7