我正在 try catch 重复的 POJO 属性或映射键已传递到我的 WebService。
例如,如果 field1
出现两次或 map 键 1
出现两次,我想抛出错误:
{
"field1" : 1,
"field1" : 2,
"map" : {
"1" : {
"fieldA" : "null",
"fieldB" : "2"
},
"1" : {
"fieldX" : "null",
"fieldY" : "2"
}
}
}
最佳答案
可以使用JsonParser.Feature.STRICT_DUPLICATE_DETECTION
ObjectMapper mapper = new ObjectMapper();
mapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
MyPOJO result = mapper.readValue(json, MyPOJO.class);
结果:
com.fasterxml.jackson.core.JsonParseException: Duplicate field 'field1'
How to use Jackson to validate duplicated properties?关于 DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY
的帖子
关于json - Jackson 检测重复的 JSON POJO 属性和 Map 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23781637/