org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported或其他Content type不支持處理


很久沒從頭到尾搭框架,今天搭的過程中,springmvc controller方法入參用@RequestBody自動綁定參數時一直提示各種 not supported

排查問題有兩個解決路徑:

1)使用post協議提交時,請檢查Content type類型,如: 

$.ajax({
    type: "POST",
    contentType: "application/json;charset=UTF-8",
    url: "/reg",
    data: JSON.stringify(data.field),
    dataType: 'json',
    success: function(result) {
        if(result.code == 0) {
            layer.msg('注冊成功!');
        } else {
            layer.msg(result.msg);
        }
    }
});

  請檢查上方contentType類型,如果想用springmvc @RequestBody注解做提交json字符串自動綁定到pojo入參時,類型需要是"application/json;charset=UTF-8",否則會拋"not supported"異常。

2)缺少jackson-databind jar包

  這個好辦,把maven或gradle的坐標加上就好,如下:

  maven:

    

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.8.1</version>
</dependency>

  gradle:

    

compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.8.1'

 

然后controller直接這么用就好了:

  

@RequestMapping(value = "/reg", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVo reg(@RequestBody user u) throws Exception {
       //其他crud邏輯
    }

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM