The problem
After the front end passes JSON format data to the back end, SpringMVC reports
org.springframework.http.converter.HttpMessageNotReadableException:
* JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token;
* nested exception is com.fasterxml.jackson.databind.JsonMappingException:
* Can not deserialize instance of java.lang.String out of START_OBJECT token
* at [Source: java.io.PushbackInputStream@6822f8aa; line: 1, column: 88] (through reference chain: com.xxx.XXXDto["employees"])
plan
To solve this problem, we can use one of the Uniform Exception Handling methods in Spring
@ExceptionHandler({HttpMessageNotReadableException.class, JsonMappingException.class, HttpMediaTypeNotSupportedException.class})
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String,Object> exceptionHandler(Exception ex){
Map<String,Object> map = new HashMap<>(3);
try{
map.put("code","400");
map.put("msg",ex.getMessage());
return map;
}catch (Exception e){
log.error("exception handler error",e);
map.put("code","400");
map.put("msg",e.getMessage());
return map;
}
}
Solve problems encountered in the process
The above method can handle the problem of JSON formatting errors, but the data content returned to the front end is in the following format
JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: java.io.PushbackInputStream@6822f8aa; line: 1, column: 88] (through reference chain: com.xxx.XXXDto["callBackUrl"])
We can’t see what the problem is at first sight, and even if we are familiar with the error, we can’t immediately find out where the problem is, because we need to process the above data and then send it back to the front end. Right
Finally solve the problem
We debug the source code found, Change is abnormal in springframework org. Springframework. HTTP. Converter. Json. AbstractJackson2HttpMessageConverter
thrown inside abnormal is
, Thrown in for springmvc
JsonMappingException extends JsonProcessingException HttpMessageNotReadableException
, as a result, we only need to add corresponding processing logic unified exception handling, can be returned to the front
@ExceptionHandler({HttpMessageNotReadableException.class, JsonMappingException.class, HttpMediaTypeNotSupportedException.class})
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String,Object> exceptionHandler(Exception ex){
Map<String,Object> map = new HashMap<>(3);
try{
if(ex instanceof HttpMessageNotReadableException
&& ex.getMessage().indexOf("JSON parse error:")>-1){
map.put("code","400");
String message=ex.getMessage();
int beginIndex=message.indexOf("XXXDto[\"");
int endIndex=message.indexOf("\"])",beginIndex);
message="parameter"+message.substring(beginIndex+22,endIndex)+" format error";
map.put("msg",message);
}else{
map.put("code","400");
map.put("msg",ex.getMessage());
}
return map;
}catch (Exception e){
log.error("exception handler error",e);
map.put("code","400");
map.put("msg",e.getMessage());
return map;
}
}
Read More:
- How to parse JSON string in.Net [error reading job object from jsonreader. Current jsonreader item is not an obj]
- [Solved] IDEA JSP File out.println Error: Cannot resolve method ‘println(java.lang.String)’
- [Solved] JSON parse error: Unexpected character (‘‘‘ (code 39)): was expecting double-quote to start ……
- [Solved] SyntaxError:JSON.parse:unexpected character at line 1 column 1 of the JSON data
- [Solved] Android Error: java.lang.IllegalStateException: Not allowed to start service Intent
- [Solved] java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell
- JSON parse error: raw timestamp (1595952000000) not allowed for
- [Solved] Interface automation test: JSON parse error
- [Solved] JSON parse error: Unexpected character (‘‘‘ (code 39)): was expecting double-quote to star
- [Solved] .java.lang.IllegalArgumentException: requirement failed: Can only call getServletHandlers on a runn
- Prometheus Error: “INVALID“ is not a valid start token [How to Solve]
- [Solved] Wwagger error: java.lang.NumberFormatException: For input string: ““
- [Solved] MongoDB Error: TypeError: Object of type ObjectId is not JSON serializable
- [Solved] swagger3 Error: org.springframework.context.ApplicationContextException: Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
- Kafka error: ERROR There was an error in one of the threads during logs loading: java.lang.NumberFormatException: For input string: “derby” (kafka.log.LogManager)
- [Solved] AS Warning: String literal in setText can not be translated. Use Android resources instead.
- Start error in maven web project java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener
- vue VM682:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0
- Sqoop Error: Can‘t parse input data: ‘\N‘ [How to Solve]