-2

I'm writing a RESTful Web service. Technologies that I use:

  • Eclipse EE Kepler IDE
  • GlassFish 3 (based on Java 6)
  • Jersey
  • JDK v7

When I annotate a Java method with, for example, the @DELETE annotation I get the following HTTP error (invoked via URI):

HTTP Status 405 - Method Not Allowed

I would like to know how to enable/disable (so that to enable/disable the above HTTP error) those methods (PUT, HEAD, etc.) and at which level it can be done (Glassfish, Web.xml, etc). As well, can you invoke all of those resource methods (annotated with HTTP method type) from either Web browser's URI, within the <form>, or stand-alone client application (non-browser)?

For example, whether or not the following config line on deployment descriptor is present, it makes no difference:

<security-constraint>
 <web-resource-collection>
    <web-resource-name>RESTfulServiceDrill</web-resource-name>
    <url-pattern>/drill/rest/resource/*</url-pattern>
    <http-method>DELETE</http-method>  
 </web-resource-collection>

Of course, one's can disable a specific resource method by throwing an exception from it (and map it to an HTTP error) as the indication of it. That would indicate that the implementation is not available, for example.

So far, only @GET and @POST (on the <form>) resource methods work out, the other annotated methods, such as @POST (via URI), @PUT, @DELETE, @OPTIONS returns the above HTTP error. And this is where my question needs solutions. Why does the mentioned resource methods cause HTTP error when the former two don't?

An example of a resource method:

@DELETE
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
@Path("/getDelete/{value}/{cat}")
public String getDelete(@PathParam("value") String value, @PathParam("cat") String cat){
    return value+" : "+cat;
}

Invoking URL:

<a href= "/RESTfulServiceDrill/rest/v6/exception/getDelete/Animal/cat">getDelete</a>

The deployment descriptor is empty, except for the above lines of XML code. So far, I made the app to work by using annotations, no Web.xml (only contains some default values, such as index.jsp files).

Any ideas out there?

4
  • 2
    I don't really understand your question. Do you have your project basically running, i.e. does any method of your REST classes work? If not there is a problem with your setup, if yes you should take this method as an example. For more help please update the question with your complete web.xml and example code. Commented Jul 10, 2016 at 17:40
  • To me, your question as a whole doesn't make much sense, hence I think the reason for all the down votes. Commented Jul 12, 2016 at 2:09
  • Could you provide examples of what you mean? Your question is pretty unclear. You specify the REST method for the appropriate path within Java using Jersey. Also - do you define a GET to that path before you define DELETE? Commented Jul 12, 2016 at 4:56
  • You need to learn about http methods. Anchor links make GET requests. Use a different client if you want to send different method requests. Or learn some javascript Commented Jul 13, 2016 at 0:46

1 Answer 1

3
+50

To my understanding, You have your REST APIs exposed and you are trying to access it from HTML <form>.Now you are able to access the GET and POST methods(REST APIs) from HTML <form> but not PUT, DELETE and other HTTP methods.

The reason why you get Method Not Allowed exception when you try to access DELETE or PUT or other HTTP methods is, HTML <form> does not support methods other than GET and POST.

Even if you try

<form method="delete"> or <form method="put"> 

HTML will not understand these methods and consider this as simply <form> (i.e) default form method is GET. So even you have mentioned method as DELETE or PUT. It is a GET request.

And when the call is made, the jersey container tries to find the requestpath(here"/getDelete/{value}/{cat}") with the specified method(here GET). Though this path exists,you have mentioned DELETE as acceptable method in your resource(@DELETE annotation says so). But Jersey is looking for GET now.Since it cant find @GET, it returns Method not allowed Exception.

So, how to solve it?

In HTML <form> you cant use HTTP methods other than GET and POST. It is better to have a wrapper in between the REST layer and HTML. So that you can make a POST call from your HTML, then the wrapper handles that call and which in-turns calls the DELETE of REST layer.

And, why POST method is not working from browser is, By default Browser makes a GET call. Have a look at Postman to make REST calls with different Http methods.

Sign up to request clarification or add additional context in comments.

4 Comments

What the wrapper is which handles calls? Is it implementation of Javascript framework or any other type of client, such as URL based (in Java 6) or specific client library in Java 7? In conclusion, does it mean that you can only invoke GET (<a href>) and POST (only from the <form>) methods from the browser as a client, otherwise, for other methods, you need to use other client implementations?
1) Better from HTML invoke Servlet or JSP, which makes DELETE call . 2) In html form u can use only GET and POST.. (form method could be either GET or Post) 3) From browser like chrome, you can make only GET calls.
That's all about I needed, more or less. By now the info provided is satisfying. Could someone take out the -1's.
See this link.. stackoverflow.com/questions/921942/… This uses Javascript(Jquery) to make REST call.. Which would be most appropriate than using Servlet or JSP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.