Spring 不接受 POST 参数,除非@RequestParam“required=false”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13055885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring not accepting a POST parameter unless @RequestParam "required=false"
提问by node42
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 (@PathVariablesmatching, responses correctly marshalled to JSON or XML based on the Accept header, etc) 100% of the time.
我正在运行 Spring 3.1.2 应用程序。我有一个带有多种方法的 RESTful servlet。GET 方法在@PathVariables100% 的情况下都非常有效(匹配、响应正确地编组为基于 Accept 标头的 JSON 或 XML 等)。
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 requiredfield in @RequestParam. This is a simplified test method I've been using to investigate:
但是 POST 方法根本不起作用。后瞎搞与转换和所有其他春季方面我能找到(全部收归修修补补)的时间,我缩小它做了required现场@RequestParam。这是我一直用于调查的简化测试方法:
@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=false:一切正常(接收并解释参数)。正如我所期望的那样工作
required=true: (or not specified, since this is the default) I consistently get the message "MissingServletRequestParameterException: Required String parameter 'test' is not present"
required=true:(或未指定,因为这是默认值)我始终收到消息“ MissingServletRequestParameterException:所需的字符串参数'test'不存在”
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 requiredand I can see the parameter is being passed. When the parameter is optional, Spring handles the it correctly.
这是切换时运行的完全相同的测试套件required,我可以看到正在传递参数。当参数是可选的时,Spring 会正确处理它。
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...
如果有人以前遇到过这个问题或有任何想法,我很想听听他们的意见。将必需的参数标记为可选,即使它有效,即使我评论它也是糟糕的自我文档。加上这种行为让我有点紧张。希望我只是在某处搞砸了一些事情......
回答by Biju Kunjummen
Your Content-Typeheader should be application/x-www-form-urlencodedI think.
你的Content-Type标题应该是application/x-www-form-urlencoded我认为的。

