I am running a Spring 3.1.2 application. I have a RESTful servlet with a number of methods. The GET methods are working fantastic (@PathVariables
matching, responses correctly marshalled to JSON or XML based on the Accept header, etc) 100% of the time.
However the POST method is simply not working. After hours of messing around with converts and every other Spring aspect I could find (all tinkering reverted), I narrowed it do to the required
field in @RequestParam
. This is a simplified test method I've been using to investigate:
@RequestMapping (value = "/bogus",
method = POST)
public @ResponseBody PassResponse bogus (
@RequestParam (value = "test", required = false) String test) {
// Just some handy garbage objects that marshal to JSON/XML
UserResponse user = new UserResponse ();
user.setName (test);
AccountDetail detail = new AccountDetail (user,null);
return new PassResponse (detail);
}
required=false: everything works (parameter is received and interpreted). Exactly as I expect it to work
required=true: (or not specified, since this is the default) I consistently get the message "MissingServletRequestParameterException: Required String parameter 'test' is not present"
Client side view:
required=true
Request URL:http://localhost:8080/internal-project/rest/bogus
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json
Connection:keep-alive
Content-Length:12
Host:localhost:8080
Request Payload
test=LALALAA
Response Headersview source
Connection:close
Content-Length:971
Content-Type:text/html;charset=utf-8
Date:Wed, 24 Oct 2012 18:41:05 GMT
Server:Apache-Coyote/1.1
required=false
Request URL:http://localhost:8080/internal-project/rest/bogus
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json
Connection:keep-alive
Content-Length:12
Host:localhost:8080
Request Payload
test=LALALAA
Response Headersview source
Content-Type:application/json;charset=UTF-8
Date:Wed, 24 Oct 2012 18:44:03 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
It is the exact same test suite being run when toggling required
and I can see the parameter is being passed. When the parameter is optional, Spring handles the it correctly.
If anyone has run across this before or has any ideas I'd love to hear them. Marking the required parameter as optional, even if it works, is terrible self documentation even if I comment it. Plus the behavior is making me a little nervous. Hopefully I just screwed something up somewhere...
Your Content-Type
header should be application/x-www-form-urlencoded
I think.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With