我有一个反序列化问题:
这是我的课:
public class Response {
private Object ResObj;
private int ResInt;
public Object getResObj() {
return ResObj;
}
public int getResInt() {
return ResInt;
}
}
我要反序列化的 JSON 是:
{"ResObj":{"ClientNum":"12345","ServerNum":"78945","IdNum":"020252"},"ResInt":0}
我得到这个异常:
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ResObj" , not marked as ignorable (0 known properties: ])
at [Source: java.io.StringReader@1f758500; line: 1, column: 20] (through reference chain: ["ResObj"])
我不想添加:
@JsonIgnoreProperties(ignoreUnknown = true)
因为我想获取 ResObj...
如果我添加注释,它会通过但它会将其设置为 null .. 这是我不想要的。
最佳答案
如果你不想在你的 bean 中有一个 setter 并且只使用字段和 getter,你可以使用 ObjectMapper
的可见性检查器来允许字段可见性。
类似以下内容:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setVisibility(VisibilityChecker.Std.defaultInstance().withFieldVisibility(JsonAutoDetect.Visibility.ANY));
关于java - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException : Unrecognized field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23469784/