关于SpringMVC的HttpMediaTypeNotSupportedException异常解决
异常信息
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直接这么用就好了:
@RequestMapping(value = "/reg", method = RequestMethod.POST)
@ResponseBody
public ResponseVo reg(@RequestBody user u) throws Exception {
//其他业务
} 异常信息 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包
