如何使用 MapStruct 忽略零值
我正在尝试根据 http 请求发送的内容更新 postgres 记录,这是不可预测的(可以是属性的任意组合)。现在,我想做的是让 MapStruct 忽略“默认”Java 值(即未包含在请求正文中的值),但它只忽略 null 值,因此不忽略默认 int 类型值(即 0)。我已经用表达式完成了它,但这不是一个非常优雅的解决方案。我也尝试过使用 Integer 类型,但 MapStruct 仍然将其视为普通 int 。我的问题是:有没有一种解决方法可以忽略等于零的属性?
I am trying to update a postgres record based on what the http request is sending, which is unpredictable (can be any combination of attributes). Now, what i want to do is to get MapStruct to ignore "default" Java values (i.e. the ones not included in the request body) but it only ignores null values, so not default int type values (which is 0). I've accomplished it with expressions but it is not a very elegant solution. I've also tried using Integer type but still MapStruct treats it like a common int. My question is: ¿Is there a workaround ignoring properties that are equal to zero?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mapstruct
1.5.0
(当前为1.5.0.Beta2
)引入了@Condition
注解。通过此功能,您可以定义自己的检查,以确定何时映射值。例如,要映射所有非
0
的int
,您可以执行以下操作:有关详细信息,请参阅 mapstruct 参考指南中的条件映射。
With mapstruct
1.5.0
(currently at1.5.0.Beta2
) the@Condition
annotation is introduced. With this you can define your own checks for when to map values.for example to map all
int
s that are not0
you could do the following:For more information see Conditional Mapping at the mapstruct reference guide.