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