英文:
Mapstruct - ignore field by name in whole project
问题
我有一个项目,经常需要映射 dto 到 db 模型。几乎所有的 db 模型都有一些额外的字段,比如 version,这些字段从 dto 中不会映射过来。
有没有可能或者优雅的方法通过字段名全局忽略目标字段呢?
目前我不得不使用 @Mapping(target = "version", ignore = true)
。不能使用 @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
,因为我希... (以下内容被截断)
英文:
I've got project, where I often map dto -> db model. Almost all my db models have some additional fields, like version, which are never mapped from dto.
Is there any possibility or elegant workaround to ignore target fields globally only by theirs names?
For now I have to use @Mapping(target = "version", ignore = true)
. Cannot use @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
, because I'd like to get warns or errors when I accidentally omit any important property. I control default behavior with '-Amapstruct.unmappedTargetPolicy=WARN'
, which I change for ERROR during developing new functions.
Some people may say that these are just warnings, but when there are many of them, it makes it harder to spot mistakes.
Mapstruct 1.3.1.Final, probably will move to 1.4.1.Final in nearest future. Searched through the docs, but couldn't find anything useful.
Thanks in advance for your answers
答案1
得分: 10
> 有些人可能会说这些只是警告,但当它们变得很多时,就更难发现错误。
首先,我很高兴您不会忽略警告。
从 MapStruct 1.4 开始,您可以创建一个由多个映射组成的注解。详细信息请阅读 MapStruct 1.4.1 的文档。3.2. 映射组合(实验性内容)。这个配置也可以被共享(11.3. 共享配置)。
@Retention(RetentionPolicy.CLASS)
@Mapping(target = "version", ignore = true)
public @interface WithoutVersion { }
@Mapper
public interface DtoMapper {
@WithoutVersion
Dto entityToDto(Entity entity);
}
这在需要忽略多个字段的情况下非常有用。最大的优点是,您通过显式使用注解而不是全局配置来控制映射。
@Retention(RetentionPolicy.CLASS)
@Mapping(target = "version", ignore = true)
@Mapping(target = "createdAt", ignore = true)
@Mapping(target = "modifiedAt", ignore = true)
@Mapping(target = "id", ignore = true)
public @interface WithoutMetadata { }
@Mapper
public interface DtoMapper {
@WithoutMetadata
Dto entityToDto(Entity entity);
}
英文:
> Some people may say that these are just warnings, but when there are many of them, it makes it harder to spot mistakes.
First of all, I am glad you don't ignore warnings.
As of MapStruct 1.4 you can create an annotation that is composed of multiple mappings. Read more at the documentation of MapStruct 1.4.1. 3.2. Mapping Composition (experimental). This configuration can also be shared (11.3. Shared configurations).
@Retention(RetentionPolicy.CLASS)
@Mapping(target = "version", ignore = true)
public @interface WithoutVersion { }
@Mapper
public interface DtoMapper {
@WithoutVersion
Dto entityToDto(Entity entity)
}
This get useful in case of multiple fields to be ignored. The biggest advantage is that you take a control over mapping through the explicit use of the annotation and not a global configuration.
@Retention(RetentionPolicy.CLASS)
@Mapping(target = "version", ignore = true)
@Mapping(target = "createdAt", ignore = true)
@Mapping(target = "modifiedAt", ignore = true)
@Mapping(target = "id", ignore = true)
public @interface WithoutMetadata { }
@Mapper
public interface DtoMapper {
@WithoutMetadata
Dto entityToDto(Entity entity)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论