[英]Spring Boot POST request with missing request body?
I have a very simple HTML form page (which is a part of Spring Boot web application in src/main/resources/public/web.html) to post a String from a textarea to a Spring Boot web application version 1.5.2.我有一个非常简单的 HTML 表单页面(它是 src/main/resources/public/web.html 中 Spring Boot Web 应用程序的一部分)将字符串从 textarea 发布到 Spring Boot Web 应用程序版本 1.5.2。
<form action="" method="post">
<textarea cols="128" rows="40" name="query"></textarea>
<input value="Send" type="submit">
</form>
And the SpringBoot class to handle the POST request:以及用于处理 POST 请求的 SpringBoot 类:
@RestController
public class QueryController {
@RequestMapping(value = "/handle", method = RequestMethod.POST)
protected void handlePost(@RequestBody String postBody) throws Exception {
// Get query from postBody here
}
}
It works with small String from textarea in client.它适用于来自客户端 textarea 的小字符串。 However, when the String is big (eg: with HTTP request header: Content-Length:3789333 (3 MB)).但是,当字符串很大时(例如:带有 HTTP 请求标头:Content-Length:3789333 (3 MB))。 Spring Boot throws an exception like this: Spring Boot 抛出这样的异常:
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: protected void QueryController.handlePost(java.lang.String) throws java.lang.Exception
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:154)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:128)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
I'm not sure what causes this problem, I'm running the web application with embedded Tomcat from Spring Boot.我不确定是什么导致了这个问题,我正在使用 Spring Boot 中的嵌入式 Tomcat 运行 Web 应用程序。
我不确定,但这可能是由于缺少内容编码造成的吗?
The problem is @RequestBody cannot get value for the big query.问题是 @RequestBody 无法获得大查询的价值。 However, get request body from HttpServletRequest it can get value但是,从 HttpServletRequest 获取请求体可以获取值
protected void handlePost(HttpServletRequest httpServletRequest) throws Exception {
String postBody = this.getPOSTRequestBody(httpServletRequest);
}
Update your contoller to following:将控制器更新为以下内容:
@RestController
public class QueryController {
@RequestMapping(value = "/handle", method = RequestMethod.POST)
protected void handlePost(@RequestParam(value="query",required=false) String query) throws Exception {
// Your code goes here
}
}
If you do not pass paramater you will get org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'query' is not present
.如果您不传递参数,您将得到org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'query' is not present
。 If paramater not required you can add required=false
to RequestParam
.如果不需要参数,您可以将required=false
添加到RequestParam
。 By default required=true
for RequestParam.默认情况下,RequestParam 为required=true
。
It depends to your server.这取决于您的服务器。 I hope your are using Tomcat
.我希望您正在使用Tomcat
。 By default server has maxPostSize
.默认服务器有maxPostSize
。 The following is for Tomcat以下是针对Tomcat的
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxPostSize="6291456" />
maxPostSize
is 6MB in my preceding code.在我前面的代码中, maxPostSize
是 6MB。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.