Java MapStruct 自动忽略未映射的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36788642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MapStruct ignore automatically unmapped properties
提问by Santiago Perez Chavez
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.
我将 MapStruct 与在我的代码中的不同业务用例之间共享的大模型(超过 50 个字段)一起使用。根据入口点,某些属性将被映射,而有些则不会。当我构建我的项目时,我总是会收到“警告:未映射的目标属性”消息。
I have researched and seen that it is possible to tell the mapstruct to ignore the field by using the semantic
我已经研究并看到可以通过使用语义告诉 mapstruct 忽略该字段
@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?
问题是,鉴于我的对象有这么多字段,忽略每个映射器类中的每个属性需要大量代码。我也不希望在我的日志中出现这个警告。有什么方法可以告诉 mapstruct 忽略未映射的内容,从而避免出现此消息?
采纳答案by Gunnar
You can set the "unmapped target policy" on the @Mapper
level or via @MapperConfig
to share a setting across several mappers:
您可以在@Mapper
关卡或通过设置“未映射的目标策略”@MapperConfig
以在多个映射器之间共享设置:
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface MyMapper {}
回答by Safargaleev Ruslan
For ignore automapping MapStruct 1.3.0.Final Reference Guide:
对于忽略自动映射 MapStruct 1.3.0.Final 参考指南:
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),默认行为将是显式映射,这意味着必须通过@Mapping 指定所有映射,并且不会在缺少目标属性时发出警告。
@BeanMapping(ignoreByDefault = true)
OneObj map(TwoObj two);