gpt4 book ai didi

spring - Spring MVC 可以为 HTTP PUT 方法提供请求参数,还是必须使用 post?我应该使用哪个来成为 RESTful?

转载 作者:IT老高 更新时间:2023-10-28 13:50:56 25 4
gpt4 key购买 nike

我有一个 Controller Action ,我认为应该是 HTTP PUT,但是当我尝试在 Controller Action 中使用 @RequestParam 时,Spring 会提示。 HTTP PUT 方法是否不允许请求参数,这就是 Spring 拒绝它的原因吗?

@RequestMapping(value = "/{helpDocumentId}/vote", method = RequestMethod.PUT)
public void voteHelpfulness(@PathVariable long helpDocumentId, @RequestParam boolean isHelpful) {
helpManager.voteOnHelpDocument(helpDocumentId, isHelpful);
}

执行时会抛出这个错误:

org.springframework.web.bind.MissingServletRequestParameterException: Required boolean parameter 'isHelpful' is not present

当然,存在 isHelpful 参数。我可以使上面的代码完美地适用于 HTTP POST,所以我知道这不是问题。

     $.ajax({
url: "/help/" + helpDocumentId + "/vote.json",
type: "PUT",
data: {
isHelpful: isHelpful
},
success: function(response) {
// ....
}
});

PUT 是正确的 http 方法吗?此操作会修改 helpDocument,但不会创建一个。

最佳答案

Since Spring 3.1,HttpPutFormContentFilter可用于处理 application/x-www-form-urlencoded 数据:

Filter that makes form encoded data available through the ServletRequest.getParameter*() family of methods during HTTP PUT requests.

The Servlet spec requires form data to be available for HTTP POST but not for HTTP PUT requests. This filter intercepts HTTP PUT requests where content type is 'application/x-www-form-urlencoded', reads form encoded content from the body of the request, and wraps the ServletRequest in order to make the form data available as request parameters just like it is for HTTP POST requests.

对于其他传入数据,例如 JSON,您需要 @RequestBody,如 JQuery, Spring MVC @RequestBody and JSON - making it work together 中所述, 以免遇到 415 Unsupported Media Type。

关于spring - Spring MVC 可以为 HTTP PUT 方法提供请求参数,还是必须使用 post?我应该使用哪个来成为 RESTful?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8250439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com