The request sent by the client was syntactically incorrect

解决 spring mvc 下post json出现HTTP Status 400 The request sent by the client was syntactically incorrect

 

当你觉得你的配置一切正常,并且无数次检查了请求json参数与javabean都是一致的,但就是报上面的错的时候,那你可能遇到跟我一样的问题。

 

跟踪Spring执行@RequestBody的过程发现底层的真实错误是:

 

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: No content to map due to end-of-input
 at [Source: org.apache.catalina.connector.CoyoteInputStream@7abd6f93; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
 at [Source: org.apache.catalina.connector.CoyoteInputStream@7abd6f93; line: 1, column: 1]

 也就是没有内容,说明参数没有传进来。

 

 

实事上参数是有的,只不过参数是通过InputStream写入的,而在过滤器或执行@RequestBody之前的任何地方你可能从流中读出了json参数,而ServletStream是不可重复读的,所以当@RequestBody再从InputStream中读参数时就为空了。

 

知道原因了就好解决,inputStream不能重复读,但你又必须读多次怎么办?

解决办法:

request.getInputStream()只能获取一次的问题

 

 

 

你可能感兴趣的:(The request sent by the client was syntactically incorrect)