默认值为另一个 bean 属性,当源为空时使用 MapStruct。

huangapple go评论81阅读模式
英文:

Default value to another bean property when Source is null MapStruct

问题

好的,以下是您提供的内容的翻译:

在MapStruct中,我有以下的Mapper:

@Mapping(source = "payload.after", target = "payload")
TargetEntity toTarget(SourceEntity source);

有时候 payload.after 是空的,我需要用另一个属性来填充,叫做 payload.before。类似这样:

@Mapping(source = "payload.after", target = "payload")
// 或者,如果 payload.after 为空
@Mapping(source = "payload.before", target = "payload")
TargetEntity toTarget(SourceEntity source);

我尝试过这样写,但没有生效:

@Mapping(source = "payload.after", target = "payload", defaultValue = "payload.before")
TargetEntity toTarget(SourceEntity source);

有没有办法实现这个需求呢?

英文:

Well, I have the following Mapper in MapStruct:

 @Mapping(source = "payload.after", target = "payload")
 TargetEntity toTarget(SourceEntity source);

Sometimes payload.after is null and I need to fill with another property, called payload.before. Something like this:

 @Mapping(source = "payload.after", target = "payload")
   //OR, if payload.after is null
 @Mapping(source = "payload.before", target = "payload")
 TargetEntity toTarget(SourceEntity source);

I tried used in this way but didn't work:

 @Mapping(source = "payload.after", target = "payload", defaultValue = "payload.before")
 TargetEntity toTarget(SourceEntity source);

Is there a way to do it?

答案1

得分: 3

从MapStruct文档中我看到,只有您可以使用预定义的值:https://mapstruct.org/documentation/stable/reference/html/#default-values-and-constants

但是您可以通过使用像@AfterMapping@BeforeMapping这样的注释来轻松实现,详细示例请查看MapStruct文档中的示例 默认值为另一个 bean 属性,当源为空时使用 MapStruct。

编辑:

@Mapping(target = "otherTargetField", source = "otherSrcField")
TargetEntity toTarget(SourceEntity source);

@AfterMapping
void toTargetAfterMapping(@MappingTarget TargetEntity, SourceEntity source) {
   // 您的映射之后的逻辑,使用 payload.before 和 payload.after
}

MapStruct会自动解析,该方法在映射方法末尾在最后一个返回语句之前被调用。如果想了解更多信息,请参阅MapStruct文档 - 使用 before 和 after 进行自定义映射

另一种方法是使用 defaultExpression

@Mapping(target = "payload", source = "payload.after", defaultExpression = "java(source.getPayload().getBefore())")
TargetEntity toTarget(SourceEntity source);
英文:

From MapStruct docs I see that only you can use predefined values: https://mapstruct.org/documentation/stable/reference/html/#default-values-and-constants

But you can easily do it by using annotations like @AfterMapping or @BeforeMapping check in MapStruct docs examples 默认值为另一个 bean 属性,当源为空时使用 MapStruct。

EDIT:

@Mapping(target = "otherTargetField", source = "otherSrcField")
TargetEntity toTarget(SourceEntity source);

@AfterMapping
void toTargetAfterMapping(@MappingTarget TargetEntity, SourceEntity source) {
   // Your after mapping logic with payload.before and payload.after
}

MapStruct auto-resolves that this method is called at the end of the mapping method before the last return statement. If you want to know more see MapStruct docs - customising mappings with before and after.

Another approach which you can use is a defaultExpression

@Mapping(target = "payload", source = "payload.after", defaultExpression = "java(source.getPayload().getBefore())")
TargetEntity toTarget(SourceEntity source);

huangapple
  • 本文由 发表于 2020年10月22日 06:01:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64472344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定