Java / MapStruct Java Interview questions part 2
What is the difference between NullValueMappingStrategy options?
NullValueMappingStrategy configures what happens when the entire source object passed to a mapping method is null, not individual properties.
| RETURN_NULL | RETURN_DEFAULT |
| If the source parameter is null, the method returns null (this is the default behavior). | If the source parameter is null, the method returns a new default-constructed target instance instead of null. |
| Suits cases where "no source" should mean "no result". | Suits cases where downstream code expects a non-null object, e.g. to avoid null checks in callers. |
The strategy is set at the mapper or method level via @Mapper(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT), and can be overridden per method when only some conversions need the alternate behavior.
More Related questions...