【问题标题】:Spring Boot POST request with missing request body?缺少请求正文的 Spring Boot POST 请求?
【发布时间】:2018-07-18 18:01:31
【问题描述】:

我有一个非常简单的 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>

以及处理 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
    }
}

它适用于客户端文本区域中的小字符串。但是,当字符串很大时(例如:带有 HTTP 请求标头:Content-Length:3789333 (3 MB))。 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)

我不确定是什么导致了这个问题,我正在使用 Spring Boot 中的嵌入式 Tomcat 运行 Web 应用程序。

【问题讨论】:

    标签: java spring spring-mvc spring-boot


    【解决方案1】:

    我不确定,但这可能是由于缺少内容的编码造成的吗?

    【讨论】:

      【解决方案2】:

      问题是@RequestBody 无法为大查询获取值。但是,从 HttpServletRequest 获取请求体可以获取值

       protected void handlePost(HttpServletRequest httpServletRequest) throws Exception {
          String postBody = this.getPOSTRequestBody(httpServletRequest); 
      
       }
      

      【讨论】:

        【解决方案3】:

        将您的控制器更新为以下内容:

        @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
            }
        }
        

        如果你没有通过参数你会得到org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'query' is not present。如果不需要参数,您可以将required=false 添加到RequestParam。 RequestParam 默认required=true

        这取决于您的服务器。我希望你使用Tomcat。默认情况下,服务器有maxPostSize。以下是针对 Tomcat

        <Connector port="8080" protocol="HTTP/1.1"
                       connectionTimeout="20000"
                       redirectPort="8443"
                       maxPostSize="6291456" />
        

        maxPostSize 在我之前的代码中是 6MB。

        【讨论】:

        • 只有当 POST 很大时才会出现另一个错误] ExceptionHandlerExceptionResolver@189:已解决由处理程序执行引起的异常:java.lang.NullPointerException 错误 [09:39:24] ExceptionAdvice@57:捕获异常:org. springframework.web.bind.MissingServletRequestParameterException:必需的字符串参数“查询”不存在
        • 大查询,查询值为null,没用。
        • 它也不行,谢谢你的尝试,我没有上传文件。
        • maxPostSize 没有帮助?
        猜你喜欢
        • 2021-09-24
        • 2019-03-29
        • 2021-07-15
        • 1970-01-01
        • 1970-01-01
        • 2020-09-29
        • 2020-04-19
        • 1970-01-01
        • 2021-01-10
        相关资源
        最近更新 更多