Spring MVC REST Json Conversion异常

ami*_*que 2 java rest spring json spring-mvc

我正在关注一个简​​单的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"])
Run Code Online (Sandbox Code Playgroud)

我收到了以下JSON

{"property":
    {
        "name":"name",
        "age":"22"
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是我的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;
}
Run Code Online (Sandbox Code Playgroud)

Property是标准的POJO,带有名字和年龄的吸气/设定器.

我该如何解决此异常?

Shi*_*abu 6

你的JSON包含 {"property": { "name":"name", "age":"22" } }

因此,JSON解析器寻找一个叫做场property(setProperty() method exactly)中的Property类.因此,您应该propertyProperty类中使用getter和setter 调用一个字段.

因此,要忽略任何不在类中的JSON解析的字段,您应该使用 @JsonIgnoreProperties(ignoreUnknown = true)

在你的课堂上

@JsonIgnoreProperties(ignoreUnknown = true)
public class Property
Run Code Online (Sandbox Code Playgroud)

因此它将忽略JSON字符串中不属于您的Property类中的任何字段.

但你的问题仍然无法解决.因为您的JSON字符串name and age属性内.所以基本上JSON解析器会查找名为property(类的对象)的字段.然后在对象内部设置名称和年龄的值.然后无需设置JSON ignore属性.

所以你有三个选择

1.创建一个对象属性称为property内部属性与getter和setter类

public class Property{
    private Property property;
    private String name;
    private int age;
    //getter and setter
}
Run Code Online (Sandbox Code Playgroud)

然后在您的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;
}
Run Code Online (Sandbox Code Playgroud)

2.为避免混淆,请使用名为property的对象创建另一个类作为对象Property.

例:

public class PropertiesJson{
private Property property
//getter and setter
}
Run Code Online (Sandbox Code Playgroud)

然后在控制器中使用它而不是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;
    }
Run Code Online (Sandbox Code Playgroud)

3.另一个选项是更改您的JSON字符串

{ "name":"name", "age":"22" }
Run Code Online (Sandbox Code Playgroud)

这就够了.如果你可以改变JSON字符串,这是更好的主意.否则,您必须选择任何其他选项.