【发布时间】:2020-03-09 10:48:50
【问题描述】:
当使用 ObjectMapper 将 json 字符串转换为对象时,我想验证它应该仅在所有 json 键与 Class 匹配时才将其转换为 Object。 例如:
Class ABC{
private String mem1;
private String mem2;
private String mem3;
}
假设 json 字符串是
{
mem1:'somevalue',
mem3:'somevalue'
}
当使用对象映射器将上述 json 字符串转换为 ABC 类时,它会在匹配 mem1 和 mem3 时进行转换,我想验证它是否会转换 json 字符串是否具有与 ABC 类相同的所有三个成员。
知道怎么做吗?我能想到的唯一方法是将json字符串转换为JSONObject,然后将键与类的字段名匹配
【问题讨论】:
-
我需要类似的解决方案,因此你可以通过这个帖子,我认为它会有所帮助:How to throw exception while converting from JsonStr to PojoClassObj if any key/value is missing in the JsonStr?
标签: java spring spring-boot objectmapper