好久沒從頭至尾搭框架,今天搭的過程當中,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邏輯 }