PostReplaceFilter 存在问题,无法使用 PUT 覆盖 POST
我在使用 PostReplaceFilter 时遇到问题。我正在尝试使用 PUT 请求覆盖从 HTML 表单发送的 POST 请求。文档说,对于 Jersey 1.10(我正在使用),我只需要使用请求标头(“X-HTTP-Method-Override”)或查询参数“_method”来发出信号压倒一切的。我选择使用“_method”方式,但它根本不起作用。
这是我的 HTML 表单:
<!DOCTYPE html>
<html>
<head>
<title>New comment</title>
</head>
<body>
<form action="http://localhost:3030/booklet/clusters/4eec2b6c0364ddde9ad0a82a/comments/COMM5372363818493721528" method="POST">
<input name="_method" type="hidden" value="PUT" />
<TEXTAREA NAME="content" COLS=40 ROWS=6>"fafafdfdsgdsg"</TEXTAREA>
<input type="submit" value="Update" />
</form>
</body>
</html>
这是我的资源:
@PUT
@Path("/{id}/comments/{comment_id}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void updateComment(
@PathParam("id") String id,
@PathParam("comment_id") String commentId,
@FormParam("content") String content,
@Context HttpServletResponse servletResponse
) throws IOException {
Comment comment = new Comment();
comment.setId(commentId);
comment.setContent(content);
dao.updateOrCreateComment(comment);
String requestURL = uriInfo.getBaseUriBuilder().build().toURL().toExternalForm() +
RESOURCE_CLUSTERS + "/" + id + "/" +
RESOURCE_COMMENTS;
servletResponse.sendRedirect(requestURL);
}
这是我的 web.xml 配置:
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.PostReplaceFilter;com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.container.filter.PostReplaceFilterConfig</param-name>
<param-value>QUERY</param-value>
</init-param>
我也尝试过:
<init-param>
<param-name>com.sun.jersey.api.container.filter.PostReplaceFilterConfig</param-name>
<param-value>HEADER,QUERY</param-value>
</init-param>
我不确定那里出了什么问题,但请求在服务器端仍然显示为 POST,而不是 PUT。如果我错过了任何重要的事情,请告诉我。
谢谢你们,
I'm having trouble with the usage of PostReplaceFilter. I'm trying to override a POST request sending from an HTML form with a PUT request. The documentation said that, for Jersey 1.10 (which I'm using), I only need to use either request HEADER ("X-HTTP-Method-Override") or query parameter "_method" to signal the overriding. I have chosen to use the "_method" way, but it doesn't work at all.
Here's my HTML form:
<!DOCTYPE html>
<html>
<head>
<title>New comment</title>
</head>
<body>
<form action="http://localhost:3030/booklet/clusters/4eec2b6c0364ddde9ad0a82a/comments/COMM5372363818493721528" method="POST">
<input name="_method" type="hidden" value="PUT" />
<TEXTAREA NAME="content" COLS=40 ROWS=6>"fafafdfdsgdsg"</TEXTAREA>
<input type="submit" value="Update" />
</form>
</body>
</html>
Here's my resource:
@PUT
@Path("/{id}/comments/{comment_id}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void updateComment(
@PathParam("id") String id,
@PathParam("comment_id") String commentId,
@FormParam("content") String content,
@Context HttpServletResponse servletResponse
) throws IOException {
Comment comment = new Comment();
comment.setId(commentId);
comment.setContent(content);
dao.updateOrCreateComment(comment);
String requestURL = uriInfo.getBaseUriBuilder().build().toURL().toExternalForm() +
RESOURCE_CLUSTERS + "/" + id + "/" +
RESOURCE_COMMENTS;
servletResponse.sendRedirect(requestURL);
}
Here's my web.xml config:
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.PostReplaceFilter;com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.container.filter.PostReplaceFilterConfig</param-name>
<param-value>QUERY</param-value>
</init-param>
I have also tried:
<init-param>
<param-name>com.sun.jersey.api.container.filter.PostReplaceFilterConfig</param-name>
<param-value>HEADER,QUERY</param-value>
</init-param>
I'm not sure what went wrong there but the request still showed up as POST, instead of PUT, on the server side. Please let me know if I missed anything important.
Thanks guys,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在 query 参数中传递方法名称,而不是表单参数。即,不要将名为“_method”的隐藏输入添加到表单中,而是将“_method”查询参数添加到表单的操作 URI 中。即操作 URI 应该是:
http://localhost:3030/booklet/clusters/4eec2b6c0364ddde9ad0a82a/comments/COMM5372363818493721528?_method=PUT
You need to pass the method name in the query parameter, not form parameter. I.e. instead of adding a hidden input named "_method" to your form, add the "_method" query parameter to your action URI for the form. I.e. the action URI should be:
http://localhost:3030/booklet/clusters/4eec2b6c0364ddde9ad0a82a/comments/COMM5372363818493721528?_method=PUT