5

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.

<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:

@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. However, when the String is big (e.g: with HTTP request header: Content-Length:3789333 (3 MB)). Spring Boot throws an exception like this:

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.

3 Answers 3

1

I'm not sure but could this be caused by missing the encoding of the content?

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is @RequestBody cannot get value for the big query. However, get request body from HttpServletRequest it can get value

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

Comments

0

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. If paramater not required you can add required=false to RequestParam. By default required=true for RequestParam.

It depends to your server. I hope your are using Tomcat. By default server has maxPostSize. The following is for Tomcat

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

maxPostSize is 6MB in my preceding code.

4 Comments

Another error only when POST is big ] ExceptionHandlerExceptionResolver@189: Resolved exception caused by Handler execution: java.lang.NullPointerException ERROR [09:39:24] ExceptionAdvice@57: Catched an exception: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'query' is not present
With big query, the query value is null, it doesn't help.
It doesn't work either, thanks for your trying, I'm not uploading file.
maxPostSize did not help?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.