我有一个反序列化问题:
这是我的课:
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 .. 我不想要的。
原文由 user2212726 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果你不想在你的bean中有一个setter并且只使用字段和getters,你可以使用
ObjectMapper
的可见性检查器—来允许字段可见性。像下面这样的东西: