使用自定义注释将空字符串转换为null
我想注释字符串字段。例如:
@EmptyToNull
String name
我不想验证该领域。在发送DTO中的字符串的空值时,我想将空白转换为null。也许杰克逊数据处理器有一个解决方案?例如:
@JsonSetter(nulls = Nulls.AS_EMPTY) *changes null to empty*
我需要带有逻辑的注释,但在任何地方都找不到示例。有人帮忙吗?
I would like to annotate the String field. E.g:
@EmptyToNull
String name
I don't want to validate the field. I would like to convert blanks to null when sending an empty value for a String in DTO. Maybe there is a solution with Jackson Data Processor? E.g:
@JsonSetter(nulls = Nulls.AS_EMPTY) *changes null to empty*
I need an annotation with logic, but can't find an example anywhere. Someone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Deserialization
Extend StringDeserializer
Use
EmptyToNull
deserializer for specific field. It will look like custom annotationAlso, you can register the deserializer globally for all strings for
ObjectMapper
Alternative solution
Implement logic in constructor
Or create custom setter for field
Serialization
Jackson provides out of box annotation
Deserialization
Extend StringDeserializer
Use
EmptyToNull
deserializer for specific field. It will look like custom annotationAlso, you can register the deserializer globally for all strings for
ObjectMapper
Alternative solution
Implement logic in constructor
Or create custom setter for field
Serialization
Jackson provides out of box annotation @JsonInclude(JsonInclude.Include.NON_EMPTY) to skip empty strings in case serialization.