Question

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,

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top