美文网首页
关于SpringMVC的HttpMediaTypeNotSupp

关于SpringMVC的HttpMediaTypeNotSupp

作者: wustmz | 来源:发表于2018-10-31 14:35 被阅读0次

异常信息

WARN  o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' 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包

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

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

@PostMapping("/reg")
public ResponseVo reg(@RequestBody user u) throws Exception {
       //其他业务
}

相关文章

网友评论

      本文标题:关于SpringMVC的HttpMediaTypeNotSupp

      本文链接:https://www.haomeiwen.com/subject/frdatqtx.html