6

I have a rather big bean (~ 100 properties) that is mapped into several smaller objects. It may occur that the smaller target objects have properties with the same name as in my source bean, but not with the same semantic meaning.

I would like MapStruct to behave in this specific case to map only what I explicitly tell using a @Mapping annotation and not perform the usual automatic mapping.

The MapStruct documentation tells me just this:

In the generated method implementations all readable properties from the source type (...) will be copied into the corresponding property in the target type (...).

I didn't find any configuration option switching this behavior off. Can it be done?

pesche
  • 3,054
  • 4
  • 34
  • 35

3 Answers3

6

Switching off implicit field mapping is possible via @BeanMapping(ignoreByDefault = true) mapping method annotation since MapStruct 1.3. From MapStruct 1.3.1 Reference Guide:

By means of the @BeanMapping(ignoreByDefault = true) the default behavior will be explicit mapping, meaning that all mappings have to be specified by means of the @Mapping and no warnings will be issued on missing target properties.

oak
  • 199
  • 1
  • 6
3

As stated in Mohamed's comment, you could ignore these properties explicitly.

There is no switch as you describe it. Personally I'd probably write that specific mapping from hand instead of explicitly configuring all the mappings through annotations. Granted, you'd still benefit from type conversion etc., so it may still be beneficial, it really depends on your use case.

Gunnar
  • 18,095
  • 1
  • 53
  • 73
  • I overlooked at first the possibility to ignore mappings; in the documentation it's mentioned only for inversed mappings... – pesche Jan 18 '16 at 14:02
-1

I had a similar problem like you say, I solved it using decorators

@Mapper
@DecoratedWith(PersonMapperDecorator.class)
public interface PersonMapper {...}

see the documentation (MapStruct 1.2.0.Final Reference Guide), chapter: 12. Customizing mappings

I hope that it is also useful for you

Javier Larios
  • 192
  • 2
  • 9