MapStruct ignore automatically unmapped properties

你。 提交于 2019-12-09 07:39:55

问题


I am using MapStruct with big models (more than 50 fields) shared between different business use cases in my code. Depending on the entry point, some properties will be mapped and some not. When I build my project, I will always get the "WARNING: Unmapped target properties" message.

I have researched and seen that it is possible to tell the mapstruct to ignore the field by using the semantic

@Mapping(target = "propName", ignore = true)

The problem is, given my objects with so many fields, it would take a lot of code to ignore each single property in each mapper class. I also do not want this Warning on my log. Is there any way to tell mapstruct to ignore what is not mapped, avoiding this message?


回答1:


You can set the "unmapped target policy" on the @Mapper level or via @MapperConfig to share a setting across several mappers:

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface MyMapper {}



回答2:


For ignore automapping MapStruct 1.3.0.Final 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.

@BeanMapping(ignoreByDefault = true)
OneObj map(TwoObj two);


来源:https://stackoverflow.com/questions/36788642/mapstruct-ignore-automatically-unmapped-properties

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!