Problem Description
2020-02-13 19:32:04.322 WARN 109508 --- [p-nio-80-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]
Custom return JSON:
Front-end code
$.ajax({
url:"khRyPfGsController.do?saveRows",
type:"post",
data:JSON.stringify(result),
dataType:"json",
success:function(data){
tip(data.msg);
if(data.success){
reloadTable();
}
}
})
Back-end code
@RequestMapping(params = "saveRows")
@ResponseBody
public AjaxJson saveRows(@RequestBody List<Map<String, Object>> list) {
//省略
}
problem analysis
If the Content-Type is set to "application / x-www-form -urlencoded; charset = UTF-8"
Whether or POST request to a GET request parameters are successfully acquired by @RequestParam
However, if the front POST request body is Json object, then, will be reported error
[Content type 'application/json' not supported]
on the contrary:
[Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]
Content-Type is set when the request is transmitted if JSON application / json or text / json time, JAVA in request.getParameter ( "") how can not receive data. This is because the Tomcat implementation class HttpServletRequest class is org.apache.catalina.connector.Request (actually org.apache.coyote.Request).
Content-Type end of the current request is Json, you can use @RequestBody this annotation to solve.
@RequestParam underlying parameters are obtained by request.getParameter manner, in other words, @ RequestParam request.getParameter and the same thing. Since use request.getParameter () parameter acquisition mode, the processing can get the value queryString embodiment, the embodiment may be post processed value of body data. Therefore, @ RequestParam may get processed value queryString embodiment, the embodiment may be post processed value of body data.
@RequestParam for processing Content-Type: application of the content / x-www-form-urlencoded encoded submission GET, POST.
It @RequestBody accept a string json object, rather than Json objects, often Json When requested object with JSON.stringify (data) will be able to fashion an object into json string.
solution
JS code plus c ontentType: "the Application / json" to
$.ajax({
url:"khRyPfGsController.do?saveRows",
type:"post",
data:JSON.stringify(result),
contentType:"application/json",
dataType:"json",
success:function(data){
tip(data.msg);
if(data.success){
reloadTable();
}
}
})
Reference article
When using SpringBoot do Javaweb, problems encountered in data exchange