我有这样的 POJO
@Data
public class Tree {
String id;
Leaf leaf;
}
如何使带有 RequestBody“Tree”的其余 Controller 接受属性叶上带有空字符串的 JSON 请求,忽略它并视为 null。 JSON 请求示例
{
"id": "foo",
"leaf": ""
}
最佳答案
您必须将 ObjectMapper
的 ACCEPT_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/