Solve the problem of @ RequestBody receiving error 415 from json object
- 2021-09-11 20:29:48
- OfStack
@ RequestBody Receive json Object Error 415
Front-end request:
$.ajax({
url: basePath() + "/index/login.do",
type : "post",
data: JSON.stringify(form),
dataType : "json",
contentType : "application/json;charset=utf8",
success: function (data) {
console.log(data);
},
error: function () {
}
});
Backend Receive:
@ResponseBody
@RequestMapping(value = "/login",method = RequestMethod.POST,produces = "application/json;charset=utf8")
public JSONObject login(@RequestBody LoginVo loginVo){
JSONObject result = new JSONObject();
UsernamePasswordToken token = new UsernamePasswordToken(loginVo.getUsername(),loginVo.getPassword());
System.out.println(loginVo.isRememberMe());
Subject subject = SecurityUtils.getSubject();
subject.login(token);
if (subject.isAuthenticated()){
result.put("result",true);
}else{
result.put("result",false);
}
return result;
}
Front-end ajax request, back-end using @ RequestBody receive, report 415 request data format error
Error cause:
springMVC cannot read the dataType set by ajax and process the request header in a corresponding way, so it cannot process json data
Solution:
Introduce Jackson related jar package in maven, and introduce related configuration in xml of springMVC. The related codes of maven and springMVC are as follows:
maven:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
springMVC:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<!-- Set the return string encoding -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- json Converter -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
The back end uses @ RequestBody to receive data from the front end
Step on the pit (1)
@ RequestBody receives json strings and can only use post submission
The front end directly copies the js of the similar function page, which is the submission method of get used
However, the front end reports an error of 500, and the back end reports an error prompt
2019-09-12 09:17:43. 088 ERROR GlobalExceptionHandler: An exception occurs within the system Required String parameter 'xxx' is not present
Step on the pit ②
After that,. get (URL, data, callback) was modified to. post (URL, data, callback).
$.post(URL,data,callback);
The required URL parameters specify the URL you want to request.
The optional data parameter specifies the data sent along with the request.
The optional callback parameter is the name of the function executed after the request is successful
However, the front end continues to report an error of 500, and the back end reports an error prompt
2019-09-12 09:23:15. 409 ERROR GlobalExceptionHandler: An exception occurs within the system: Content type 'application/x-www-form-urlencoded; charset=UTF-8' not supported
Step on the pit ③
Backend hints do not support Content type as' application/x-www-form-urlencoded; The format of charset=UTF-8 'is checked by Baidu. post (URL, data, callback) is only pre-configured. The shortcut of ajax call cannot modify the type of contentType
So modify the $. post method to & ajax method
Settings
type: " post " ,
url: ctx + url,
data: JSON.stringify(allData),
dataType: " json " ,
contentType: " application/json;charset=utf-8 " ,