如果使用PUT,SpringMVC无法识别请求主体参数 [英] SpringMVC is not recognizing request body parameters if using PUT
问题描述
也许这应该行不通,但至少我想了解为什么会这样.我在PUT
主体中传递了一个简单的val = somevalue,但是spring似乎发回了400 Bad Request
,因为它似乎无法识别val参数.
Maybe this is supposed to not work, but at least I'd like to understand why then. I am passing a simple val=somevalue in the PUT
body but spring sends back a 400 Bad Request
as it does not seem to recognise the val parameter.
类似的请求适用于POST
.可能是 SpringMVC 无法识别PUT
请求主体作为参数源吗?
Similar request works with POST
. Could it be SpringMVC is not recognizing the PUT
request body as source for parameters?
Content=-Type
均已正确设置为application/x-www-form-urlencoded.
Content=-Type
is set correctly to application/x-www-form-urlencoded in both cases.
spring拒绝调用的方法是这样的:
The method that spring refuses to call is this:
@RequestMapping(value = "config/{key}", method = RequestMethod.PUT)
@ResponseBody
public void configUpdateCreate(final Model model, @PathVariable final String key, @RequestParam final String val,
final HttpServletResponse response) throws IOException
{
//...
}
为了完整起见,这是jquery ajax调用.我看不出有什么问题.客户端是Firefox 4或Chrome,两者显示的结果相同.
For completeness, here is the jquery ajax call. I cannot see anything wrong with that. Client is Firefox 4 or Chrome, both show the same result.
$.ajax({
url:url,
type:'PUT',
data:'val=' + encodeURIComponent(configValue),
success: function(data) {...}
});
有什么想法吗?
推荐答案
目前我还不知道要解决的方法,但是这里的错误报告是无法修复".我一直在争取同一个问题
I don't know of a work around at this point, but here is the bug report that is a "Won't Fix." I've been fighting the same issue
https://jira.springsource.org/browse/SPR-7414
更新:这是我的解决方法.我正在使用RequestBody注释.然后使用MultiValueMap.
Update: Here is my fix. I'm using RequestBody annotation. Then using MultiValueMap.
http://static.springsource.org /spring/docs/3.0.5.RELEASE/reference/mvc.html#mvc-ann-requestbody
@RequestMapping(value = "/{tc}", method = RequestMethod.PUT)
public void update(@PathVariable("tc") final String tc,
@RequestBody MultiValueMap<String,String> body, HttpServletResponse response) {
String name = body.getFirst("name");
// more code
}
这篇关于如果使用PUT,SpringMVC无法识别请求主体参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!