10

I have Spring MVC application with this controller method.

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addNumber(@RequestParam(value="number", required=false) Long number) {
   ...
   return "redirect:/showAll/";
}

In my JSP I have a standard HTML form which is posting a value named "number" to the controller method above. However, if I leave out the value (do not enter anything into the text field) and POST the data to the controller, before the controller method is called my browser shows

HTTP Status 400 - Required Long parameter 'number' is not present

although the controller method annotation clearly defines the "number"-parameter as not required.

Does anyone have a slight idea of what could be going on?

Thank you.

PS: The exception that is being thrown is as follows:

org.springframework.web.bind.MissingServletRequestParameterException: Required Long parameter 'number' is not present

EDIT: This is a Spring 3.2.3.RELEASE bug ( see here). With version 3.1.4.RELEASE I do not have this problem anymore.

AlexLiesenfeld
  • 2,872
  • 7
  • 36
  • 57

2 Answers2

20

I came across the same situation, and this happens when your parameter is present in the request with an empty value.

That is, if your POST body contains "number=" (with empty value), then Spring throws this exception. However, if the parameter is not present at all in the request, it should work without any errors.

Yohan Liyanage
  • 6,840
  • 3
  • 46
  • 63
  • Yeah, this is a total bug right? Still getting this issue in 2014. Is it supposed to do this? – Fraggle Dec 18 '14 at 04:41
  • 2
    This happens in Spring 4.1, too. However, if you set the `defaultValue` in the `RequestParam` annotation, no exception is thrown. – Giulio Piancastelli Apr 08 '15 at 13:04
  • In my code, I am receiving a JSON object and the problem was that does not exists a default constructor in this object. After create it, works fine! – deldev Oct 27 '15 at 18:27
1

My problem was that some of the headers in a request I was sending with Postman were not present (were unchecked):

enter image description here

When I checked back the Content-Length header, the request worked fine (200 OK response).

parsecer
  • 4,758
  • 13
  • 71
  • 140