users@jersey.java.net

Re: [Jersey] DELETE method returns 405 from browser but returns ok from client

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Thu, 29 May 2008 17:37:48 -0400

On May 29, 2008, at 5:22 PM, g f wrote:
>
> I have another problem wr to the delete method.
> It works fine from wiztools restclient however when I try to perform
> a delete through the browser,
> I get a 405 The specified HTTP method is not allowed for the
> requested resource ().
> Perhaps I am misunderstanding the capabilities of the delete method?
> Here is my code:
> @DELETE
> @Path("/delete/{arg1}")
> public Response delete(@PathParam("id")
> String id) {
> MultivaluedMap<String, String> params =
> context.getTemplateParameters();
> String s =
> params.getFirst("arg1");
> return fpkSent.deleteFPKSent(s);//returns a Response Object
> }
> Now I access the following url to perform the delete:
> http://localhost(tomcat6.1):8084/fpk/delete/recordtodelete
>
If you put that URL into a browser, it will use the HTTP GET method,
not DELETE. Its returning a 405 (Method not allowed" status because
your class doesn't have a method annotated with @GET.

You can issue a DELETE from a browser using JavaScript and
XmlHttpRequest or you can overload a POST method. You shouldn't use a
GET method to delete stuff since GET is supposed to be safe.

> One other problem I am having is accessing the PathParam...as you
> can see I am using the TemplateParameters to retrieve the args.
> In other words, if I do a System.out.println("PathParam id="+id) It
> returns null.
> Both of these problems are minor.

The value of the @PathParam has to match the path variable name. So,
e.g. in your code above you have

@Path("delete/{arg1}")

so to get the value of the "arg1" path parameter you need to use:

@PathParam("arg1")

Marc.

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.