简体   繁体   English

MapStruct 如果源属性是 null 使用替代属性

[英]MapStruct if source property is null use alternative property

Is there a simple way to conditionally use a different source property if the first property is null?如果第一个属性是 null,是否有一种简单的方法可以有条件地使用不同的源属性? I am looking for something like this:我正在寻找这样的东西:

@Mapping(target = "myTargetProperty", source = "mySourceProperty", alternativeWhenNull = "myAltSourceProperty")

You can use defaultValue or defaultExpression to specify a value if the source property is null如果源属性为 null,则可以使用defaultValuedefaultExpression指定值

For your specific use case you can use defaultExpression = "java(source.getAlternativeProperty())对于您的特定用例,您可以使用defaultExpression = "java(source.getAlternativeProperty())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM