[英]resolving com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field
[英]com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field
我有一個反序列化問題:
這是我的課:
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));
您需要 Setter 方法來允許 Jackson 設置屬性,並且您需要將 json 中的字段更改為以小寫字母開頭:
public class Response {
private Object ResObj;
private int ResInt;
public Object getResObj() {
return ResObj;
}
public void setResObj(Object ResObj) {
this.ResObj = ResObj;
}
// ...
}
和:
{"resObj":{"clientNum":"12345","serverNum":"78945","idNum":"020252"},"resInt":0}
JSON 更改的原因是 Jackson bean 序列化將反映在類上,當它看到 getXyz() 和 setXyz() 方法時,會將這些映射到 Json 文件名稱“xyz”(而不是“Xyz”)。
我認為有幾種方法可以覆蓋這種行為,一種是使用 Jackson 注釋之一。
我想你應該試試這個
public class Response {
@JsonProperty
private Object ResObj;
@JsonProperty
private int ResInt;
public Object getResObj() {
return ResObj;
}
public int getResInt() {
return ResInt;
}
}
它將通過 UnrecognizedPropertyExceptions 解決您的問題
public class Response {
public Object ResObj;
public int ResInt;
public Object getResObj() {
return ResObj;
}
public int getResInt() {
return ResInt;
}
}
使用它來解決上述問題。
我已經使用 Jackson 庫對這個問題進行了排序。 這是我的代碼片段。
**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.
我想以上,但在我的案件中提到的所有方面,這僅僅是擔任回答解決此解決方案2.在這里,我已經啟用@EnableWebMvc
在我的春天啟動應用程序。
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Autowired
private ObjectMapper objectMapper;// created elsewhere
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
// this will add a 2nd MappingJackson2HttpMessageConverter
converters.add(new MappingJackson2HttpMessageConverter(this.objectMapper));
}
}
您需要為 ResObj {"ClientNum":"12345","ServerNum":"78945","IdNum":"020252"} 中的信息定義另一個類。 否則傑克遜無法確定如何反序列化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.