Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapStruct ignore all unmapped properties for specific method

Tags:

java

mapstruct

There are several ways to ignore unmapped target properties in mapstruct.

  1. We could list all properties to ignore for specific method:
@Mapping(target = "propName", ignore = true)
  1. Or specify to ignore all properties on mapper level :
@Mapper(
        unmappedTargetPolicy = ReportingPolicy.IGNORE
)

Is there a way to mix these approaches and ignore all properties at the method level without explisently list all of them?

like image 509
Aliaksei Stadnik Avatar asked Jun 10 '26 19:06

Aliaksei Stadnik


2 Answers

Using the BeanMapping annotation you can specify this at method level.

@BeanMapping(unmappedTargetPolicy = ReportingPolicy.IGNORE)

The javadoc can be found here.

like image 148
Ben Zegveld Avatar answered Jun 12 '26 09:06

Ben Zegveld


if you are using version 1.4, you can annotate your mapping method with @BeanMapping(ignoreByDefault = true)

like image 23
Slimane Dev Avatar answered Jun 12 '26 09:06

Slimane Dev