[英]Spring MVC REST Json Conversion exception
我正在關注一個簡單的Spring MVC REST示例。 在PUT請求中,我遇到以下異常:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "property" (Class domain.Property), not marked as ignorable
at [Source: org.apache.catalina.connector.CoyoteInputStream@75280b93; line: 1, column: 14] (through reference chain: domain.Property["property"]);
nested exception is org.codehaus.jackson.map.exc.UnrecognizedPropertyException:
Unrecognized field "property" (Class domain.Property), not marked as ignorable
at [Source: org.apache.catalina.connector.CoyoteInputStream@75280b93; line: 1, column: 14] (through reference chain: domain.Property["property"])
我收到了以下JSON
{"property":
{
"name":"name",
"age":"22"
}
}
以下是我的REST方法:
@RequestMapping(method = RequestMethod.PUT, value = "/{id}")
public ResponseEntity<Property> updateProperty(@RequestBody Property property,
@PathVariable String id) {
final ResponseEntity<Property> response =
new ResponseEntity<Property>(property, HttpStatus.OK);
return response;
}
該Property
是標准POJO,其名稱和年齡均為吸氣劑/二者。
我該如何解決此異常?
您的JSON包含{"property": { "name":"name", "age":"22" } }
因此,JSON解析器在Property
Class中搜索名為property(setProperty() method exactly)
的字段property(setProperty() method exactly)
。 因此,您應該在Property
Class中有一個名為property
的字段,其中包含getter和setter。
因此,要忽略任何不在類中的JSON解析的字段,您應該使用@JsonIgnoreProperties(ignoreUnknown = true)
注釋該類@JsonIgnoreProperties(ignoreUnknown = true)
在你的課堂上
@JsonIgnoreProperties(ignoreUnknown = true)
public class Property
因此它將忽略JSON字符串中不屬於您的Property類中的任何字段。
但你的問題仍然無法解決。 因為您的JSON字符串name and age
是屬於內部屬性 。 所以基本上JSON解析器將查找名為property
字段(類的對象)。 然后在對象內部設置名稱和年齡的值。 然后無需設置JSON ignore屬性。
所以你有三個選擇
1.使用getter和setter在Property類中創建一個名為Property的 property
對象
public class Property{
private Property property;
private String name;
private int age;
//getter and setter
}
然后在您的Controller類中
public ResponseEntity<Property> updateProperty(@RequestBody Property property,
@PathVariable String id) {
Property property2=new Property();
property2=property.getProperty();
//Get Strings from normal object property2
String name = property2.getName();
int age = property2,getAge();
final ResponseEntity<Property> response =
new ResponseEntity<Property>(property, HttpStatus.OK);
return response;
}
2 。 為避免混淆,請使用名為property的字段作為Property
對象創建另一個類。
例:
public class PropertiesJson{
private Property property
//getter and setter
}
然后在控制器中使用它而不是Property
public ResponseEntity<Property> updateProperty(@RequestBody PropertiesJson propertiesJson,
@PathVariable String id) {
Property property=propertiesJson.getProperty();
//Get Strings from normal object property
String name = property.getName();
int age = property.getAge();
final ResponseEntity<Property> response =
new ResponseEntity<Property>(property, HttpStatus.OK);
return response;
}
3 。 另一個選項是更改您的JSON字符串
{ "name":"name", "age":"22" }
這就夠了。 如果你可以改變JSON字符串,這是更好的主意。 否則,您必須選擇任何其他選項。
JSON
包含一個名為property
,該屬性不能映射到Property
類的property
字段。 更改JSON
以省略此字段。 如果您無法更改JSON
以刪除property property
,請為Property
類創建一個包裝器。
class PropertyWrapper(){
private Property property;
public Property getProperty(){
return property;
}
public Property setProperty(Property p){
this.property = property;
}
}
然后在控制器中使用PropertyWrapper
:
public ResponseEntity<Property> updateProperty(@RequestBody PropertyWrapper property,
@PathVariable String id) {
final ResponseEntity<Property> response =
new ResponseEntity<Property>(property.getProperty(), HttpStatus.OK);
return response;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.