2014-01-12 67 views
2

我正在遵循一個簡單的Spring MVC REST示例。在PUT請求,我得到以下異常:Spring MVC REST Json轉換異常

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "property" (Class domain.Property), not marked as ignorable 
    at [Source: [email protected]; 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: [email protected]; line: 1, column: 14] (through reference chain: domain.Property["property"]) 

我收到以下JSON

{"property": 
    { 
     "name":"name", 
     "age":"22" 
    } 
} 

以下是我的休息方法:

@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吸氣劑/二傳手姓名和年齡。

如何解決此異常?

+0

請發佈您的「財產」類代碼。 –

+1

你的JSON包含一個「屬性」值,屬性只包含''name「和」age「'你應該從你的JSON中刪除屬性。它應該只包含'{「name」:「name」,「age」:「22」}' –

回答

2

JSON包含一個名爲property的屬性,該屬性無法映射到Property類中的property字段。更改JSON以省略此字段。如果您無法更改JSON以刪除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; 
} 
6

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

所以JSON解析器尋找一個字段中Property類稱爲property(setProperty() method exactly)。所以你應該有一個叫做property的字段,在你的Property類中有getter和setter。

所以忽略任何領域通過JSON解析這是不是在課堂上,你應標註與@JsonIgnoreProperties(ignoreUnknown = true)

在你的類的類這將是

@JsonIgnoreProperties(ignoreUnknown = true) 
public class Property 

所以它會忽略任何領域在JSON字符串中,這不屬於你的Property類。

但仍然是你的問題不會得到解決。因爲你的JSON字符串name and age屬性。所以基本上JSON解析器將尋找名爲property(該類的對象)的字段。然後在對象內部設置名稱和年齡的值。那麼不需要設置JSON忽略屬性。

所以,你有三種選擇

創建的一個對象屬性稱爲property屬性類的getter和setter

public class Property{ 
    private Property property; 
    private String name; 
    private int age; 
    //getter and setter 
} 

,然後在裏面控制器

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; 
} 

。爲避免混淆,請創建另一個類,其名稱爲屬性作爲Property的對象。

實施例:

public class PropertiesJson{ 
private Property property 
//getter and setter 
} 

然後,在控制器用它代替屬性

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; 
    } 

。另一種選擇是改變你的JSON字符串

{ "name":"name", "age":"22" } 

這就夠了。如果你可以改變JSON字符串,這是個好主意。否則,你必須選擇任何其他選項。

+0

謝謝Shiju。你的回答也是正確的。由於更明確的分離,我將凱文答覆標記爲答覆。 – amique