2017-02-23 86 views
0

我嘗試解析在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

我搜索所有可能的解決方案包括:

  1. 刪除消耗屬性。
  2. 檢查2名具有相同名稱但類型不同的setter。不,1個setter/1個吸氣劑,用於所有屬性。

還測試了手動序列化沒有問題:

Category userFromJSON = mapper.readValue("{\"id\":\"1\",\"name\":\"PIZZA\",\"priority\":\"1\",\"color\":\"\",\"categoryId\":\"\"}", Category.class); 
+0

在控制器類上面使用@RestController。 – SachinSarawgi

回答

0

韋爾普..我想哭......幾個小時後,改:

MediaType.APPLICATION_JSON_VALUE

for

Media Type.APPLICATION_JSON_UTF8_VALUE

工作人員給我。

0

聲音,Spring無法創建合適的HTTPMessageConverter,由於某種原因能夠解析JSON。如果傑克遜在類路徑上 - 因爲它包含在你的pom-xml中 - 轉換器應該在方法org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.addDefaultHttpMessageConverters

中註冊。我建議檢查,爲什麼沒有發生。或者,您可以手動配置適當的HttpMessageConverter。如果你有一個從org.springframework.web.servlet.config.annotation.WebMvcConfigurer派生的配置類,你可能會這樣:

@Override 
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 
    super.configureMessageConverters(converters); 
    converters.add(new MappingJackson2XmlHttpMessageConverter()); 
    converters.add(new MappingJackson2HttpMessageConverter()); 
} 
相關問題