Jersey returns HTTP Status 405 - Method Not Allowed
Asked Answered
U

5

21

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.

Urfa answered 18/7, 2012 at 22:3 Comment(2)
How are you requesting the URL? e.g. - post the actual URL you are trying to use.Cyclonite
The URL I am using is: localhost:8080/mydataUrfa
U
12

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>
Urfa answered 19/7, 2012 at 9:37 Comment(0)
A
6

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

Akee answered 19/7, 2012 at 9:4 Comment(0)
E
4

I think it's probably because you specified

@Produces(MediaType.APPLICATION_JSON).

1) Maybe you should try running the following in a terminal

curl -i -X GET -H 'Accept:application/json' http://localhost:8080/mydata

2) Or try change to

@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_HTML})
Ecclesiastes answered 19/7, 2012 at 7:47 Comment(2)
Thanks for the suggestion, unfortunately both of these still result in a 405 being returned.Urfa
@Urfa sorry, can you try method 1 again? I realized that I copy and pasted the wrong headerEcclesiastes
U
4

another possible source of such problem: be very attentive with all the params and annotations. I was having such problem, when I was calling a method with POST, while it was annotated with @PUT

Undine answered 4/5, 2018 at 5:31 Comment(0)
A
-7

I was suffering the same problem; http://localhost:8080/mypath/myendpoint wasn't working.

Try adding "/rest" to the path, as in http://localhost:8080/rest/mypath/myendpoint

Ambient answered 18/5, 2015 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.