org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=U

好久没从头至尾搭框架,今天搭的过程当中,springmvc controller方法入参用@RequestBody自动绑定参数时一直提示各类 not supportedajax

排查问题有两个解决路径:spring

1)使用post协议提交时,请检查Content type类型,如: json

$.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"异常。mvc

2)缺乏jackson-databind jar包app

  这个好办,把maven或gradle的坐标加上就好,以下:框架

  maven: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:post

    

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

 

而后controller直接这么用就行了:gradle

  

@RequestMapping(value = "/reg", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVo reg(@RequestBody user u) throws Exception {
       //其余crud逻辑
    }
相关文章
相关标签/搜索