码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-05-31 16:33:05      阅读:691      评论:0      收藏:0      [点我收藏+]

标签:处理   异常   坐标   post   自动   http   version   提交   dex   

很久没从头到尾搭框架,今天搭的过程中,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逻辑
    }

 



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

标签:处理   异常   坐标   post   自动   http   version   提交   dex   

原文地址:http://www.cnblogs.com/dbaxyx/p/6924459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!