我嘗試解析在POST方法的簡單對象,但我總是收到此錯誤:內容類型「應用/ JSON的」 Spring MVC中不支持POST
2017-02-23 03:52:45 DEBUG AnnotationMethodHandlerExceptionResolver:133 - Resolving exception from handler [[email protected]]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
2017-02-23 03:52:45 DEBUG ResponseStatusExceptionResolver:133 - Resolving exception from handler [[email protected]]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
2017-02-23 03:52:45 DEBUG DefaultHandlerExceptionResolver:133 - Resolving exception from handler [[email protected]]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
在我的pom.xml我已經包括了傑克遜庫文件:
...
<properties>
<jackson.version>2.6.3</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
...
而且我申報的控制器是這樣的:
/**
*
* @param request
* @return
* @return
*/
@ResponseBody
@RequestMapping(value = "/uiupdatepost", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> UIUpdatePost(@RequestBody Category category, @RequestHeader HttpServletRequest request) {
try {
return ResponseEntity.ok("");
} catch (Throwable t) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new UIError(t).toHTML());
}
}
類類是非常簡單的:
public class Category implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public Category() {
//
}
private String id;
private String name;
private String priority;
private String categoryId;
private String color;
/**
ALL GETTER AND SETTERS
**/
}
如果我捲曲運行:
curl -H "Content-Type: application/json" -X POST -d '{"id":"1","name":"PIZZA","priority":"1","color":"","categoryId":""}' http://localhost:8080/food/uiupdatepost.html
我得到這個錯誤:
HTTP:415
我搜索所有可能的解決方案包括:
- 刪除消耗屬性。
- 檢查2名具有相同名稱但類型不同的setter。不,1個setter/1個吸氣劑,用於所有屬性。
還測試了手動序列化沒有問題:
Category userFromJSON = mapper.readValue("{\"id\":\"1\",\"name\":\"PIZZA\",\"priority\":\"1\",\"color\":\"\",\"categoryId\":\"\"}", Category.class);
在控制器類上面使用@RestController。 – SachinSarawgi