Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey returns HTTP Status 405 - Method Not Allowed

I have a very simple endpoint using Jersey. My URL is static, it doesn't require any request parameters. It looks like this:

@GET
@Path("/mydata")
@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)  
public String getData()  {
  return "{'name': 'value'}";
}

However, whenever I request this URL, I always receive a HTTP Status code of 405 - Method Not Allowed.

The weird thing is, that if I change the @Path annotation and define a path variable e.g. @Path("/chart/{blah}") it works fine.

Does anyone have an idea why I have to define a path variable to get this to work? I don't need a path variable and it seems silly to add one just to get a 200 response.

like image 431
seedhead Avatar asked Jul 18 '12 22:07

seedhead


People also ask

Why am I getting a 405 error?

The 405 Method Not Allowed error occurs when the web server is configured in a way that does not allow you to perform a specific action for a particular URL. It's an HTTP response status code that indicates that the request method is known by the server but is not supported by the target resource.

Why post method is not allowed?

The Request Method' POST' Not Supported error is caused by a mismatch of the web browser configuration and the browser's URL format. In this case, the browser sends a URL request, the web server receives and recognizes the URL but cannot execute commands or grant access to the requested page.


2 Answers

Thanks for the suggestions. It ended up being me stupidly entering an incorrect url-pattern for my jersey SpringServlet. It was / instead of /*

<servlet>
   <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
</servlet>

<url-pattern>/*</url-pattern>

<servlet-mapping>
   <servlet-name>Chart Service</servlet-name>
   <url-pattern>/*</url-pattern>
</servlet-mapping>
like image 63
seedhead Avatar answered Sep 30 '22 20:09

seedhead


Annotate the class instead of the method:

@Path("/mydata")
public class MyClass(){

@GET
@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)  
public String getData()  {
  return "{'name': 'value'}";
}

}

I don't know why but it also gives me problems the other way

like image 42
Eugenio Cuevas Avatar answered Sep 30 '22 19:09

Eugenio Cuevas