gpt4 book ai didi

java - Jackson - 接受对象属性上的空字符串

转载 作者:行者123 更新时间:2023-12-02 10:52:39 24 4
gpt4 key购买 nike

我有这样的 POJO

@Data
public class Tree {
String id;
Leaf leaf;
}

如何使带有 RequestBody“Tree”的其余 Controller 接受属性叶上带有空字符串的 JSON 请求,忽略它并视为 null。 JSON 请求示例

 {
"id": "foo",
"leaf": ""
}

最佳答案

您必须将 ObjectMapperACCEPT_EMPTY_STRING_AS_NULL_OBJECT 属性设置为 true

请参阅documentation :

Determines whether empty String value is accepted as null value for regular POJOs ("beans") with data-binding: this can be useful when dealing endpoints written in a language that has loose typing and may represent missing objects as Empty Strings.

如果您使用 Spring Boot,则可以 find details here了解如何配置您的ObjectMapper

关于java - Jackson - 接受对象属性上的空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52041620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com