Java com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23469784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
com.fasterxml.Hymanson.databind.exc.UnrecognizedPropertyException: Unrecognized field
提问by user2212726
i got a deserialization problem:
我遇到了一个反序列化问题:
This is my class:
这是我的课:
public class Response {
private Object ResObj;
private int ResInt;
public Object getResObj() {
return ResObj;
}
public int getResInt() {
return ResInt;
}
}
the JSON i want to deserialize is:
我要反序列化的 JSON 是:
{"ResObj":{"ClientNum":"12345","ServerNum":"78945","IdNum":"020252"},"ResInt":0}
I get this exception:
我得到这个例外:
Exception in thread "main" com.fasterxml.Hymanson.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"])
I don't want to add:
我不想补充:
@JsonIgnoreProperties(ignoreUnknown = true)
because I want to get the ResObj...
因为我想得到 ResObj ...
if I add the annotation, it pass but it will set it as null .. which I don't want.
如果我添加注释,它会通过但它会将其设置为 null .. 我不想要。
采纳答案by Jay
If you dont want to have setter in your bean and only use fields and getters, you can use visibility checker of ObjectMapper to allow field visibility. Something like following
如果你不想在你的 bean 中有 setter 并且只使用字段和 getter,你可以使用 ObjectMapper 的可见性检查器来允许字段可见性。像下面这样的东西
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(JsonAutoDetect.Visibility.ANY));
回答by user3588826
You need to define another class for the information within ResObj {"ClientNum":"12345","ServerNum":"78945","IdNum":"020252"}. Otherwise Hymanson cannot determine how to deserialize.
您需要为 ResObj {"ClientNum":"12345","ServerNum":"78945","IdNum":"020252"} 中的信息定义另一个类。否则Hyman逊无法确定如何反序列化。
回答by jon-hanson
You need Setter methods to allow Hymanson to set the properties, and you need to change the fields in the json to begin with a lower case letter:
您需要 Setter 方法来允许 Hymanson 设置属性,并且您需要将 json 中的字段更改为以小写字母开头:
public class Response {
private Object ResObj;
private int ResInt;
public Object getResObj() {
return ResObj;
}
public void setResObj(Object ResObj) {
this.ResObj = ResObj;
}
// ...
}
and:
和:
{"resObj":{"clientNum":"12345","serverNum":"78945","idNum":"020252"},"resInt":0}
The reason for the JSON change is that the Hymanson bean serialisation will reflect over the class, and when it sees getXyz() and setXyz() methods will map these to a Json filed names "xyz" (and not "Xyz").
JSON 更改的原因是 Hymanson bean 序列化将反映在类上,并且当它看到 getXyz() 和 setXyz() 方法时,会将这些映射到 Json 文件名称“xyz”(而不是“Xyz”)。
I think there are several ways to override this behaviour, one is to use the one of the Hymanson annotations.
我认为有几种方法可以覆盖这种行为,一种是使用 Hymanson 注释之一。
回答by Ashish
I think you should try this
我想你应该试试这个
public class Response {
@JsonProperty
private Object ResObj;
@JsonProperty
private int ResInt;
public Object getResObj() {
return ResObj;
}
public int getResInt() {
return ResInt;
}
}
It will resolve your issue with UnrecognizedPropertyExceptions
它将通过 UnrecognizedPropertyExceptions 解决您的问题
回答by Bhupesh Kumar
public class Response {
public Object ResObj;
public int ResInt;
public Object getResObj() {
return ResObj;
}
public int getResInt() {
return ResInt;
}
}
Use this to resolve the above issue.
使用它来解决上述问题。
回答by Atul Sharma
I have sorted this problem using the Hymanson library. Here is my code snippet.
我已经使用 Hymanson 库对这个问题进行了排序。这是我的代码片段。
**Main Class with JSON String in all lower case:**
public class MainClass {
public static void main(String[] args) throws JsonParseException,
JsonMappingException, IOException {
String jsonStr = "{\r\n" + " \"resObj\": {\r\n" + " \"clientNum\":
\"12345\",\r\n"
+ " \"serverNum\": \"78945\",\r\n" + " \"idNum\":
\"020252\"\r\n" + " },\r\n"
+ " \"resInt\": 0\r\n" + "}";
ObjectMapper mapper = new ObjectMapper();
MyPojo details = mapper.readValue(jsonStr, MyPojo.class);
System.out.println("value of clientNum: " + details.getResObj().getClientNum());
System.out.println("value of getServerNum: " +
details.getResObj().getServerNum());
System.out.println("value of getIdNum: " + details.getResObj().getIdNum());
System.out.println("value of getResInt: " + details.getResInt());
} }
**MyPojo Class:**
public class MyPojo {
private ResObj resObj;
private String resInt;
public ResObj getResObj() {
return resObj;
}
public String getResInt() {
return resInt; } }
**ResObj class:**
public class ResObj {
private String serverNum;
private String idNum;
private String clientNum;
public String getServerNum() {
return serverNum;
}
public String getIdNum() {
return idNum;
}
public String getClientNum() {
return clientNum;
} }
**RESULT**
value of clientNum: 12345
value of getServerNum: 78945
value of getIdNum: 020252
value of getResInt: 0
NOTE: I have removed Setters in classes & there is no effect on the result.