Skip field globally in ModelMapper

I am using ModelMApper to render objects from DTO to Impl - when deserializing an object. This is done in conjunction with JAxRS. When the user makes a POST / PUT request, I don't want the "id" to ever be displayed.

I need to skip the "id" field for all mappings. I don't want to do this one by one, as there are no explicit maps for all classes. How can I configure the mapping to pass all "id" fields from all DTOs into Impls mappings.

thank

+3


source to share


2 answers


One approach is to use Conditions to conditionally display (or not) the 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 up a condition skipIds

that will be used globally for all properties so that the mapping of values ​​to any destination property named "id" will be skipped.

+7


source


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

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

      



to

public boolean applies(MappingContext context)

      

+3


source







All Articles