Vjeetje
Vjeetje

Reputation: 5384

Always map null object to null

I'm looking for a way to map every object that is null to null instead of "org.dozer.MappingException: Source object must not be null" error. I don't want to enumerate every class and say that null maps to null, I want to specify this as a general rule.

Upvotes: 4

Views: 10373

Answers (1)

Dmitry Buzdin
Dmitry Buzdin

Reputation: 1233

This one is a generic exception saying that you should not pass null object as a top level bean to mapper.map(src, dest) method. So putting guard check before invoking Dozer should help.

if (src == null) return null;
return dozer.map(src, dest);

In addition, 'map-null' policy is enabled by default, but it applies only to elements inside the bean you want to map (not top-level). So Dozer will properly map 'user.id' if it is null value. In the next version there will be a possibility to apply 'map-null' on global level as well without specifying each class. However this would only help to disable null mapping, since it is enabled by default.

Upvotes: 6

Related Questions