Reputation: 87
I have been working on simple RESTFul Web Services lately, with jersey on Eclipse. I can get the GET Functions to work just fine. But when I try POST Methods, using the Post annotation and deploy it, I get the "method not allowed" message. I can't seem to figure out why. Any idea on how I can get it to work?
Thank you.
Here is my Code:-
package myapp;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
@Path("login")
public class SimpleClass {
@POST
@Path("/sayhello")
@Produces("text/plain")
public String sayHello(@QueryParam("username") String username)
{
return "Hello "+username;
}
}
And accessed through the link as: http://localhost:8080/RestSample/login/sayhello?username=som
And I get the "method not allowed" message.
Upvotes: 0
Views: 697
Reputation: 130867
When typing the resource URL in the browser address bar, you are performing a GET
request. That's the default behavior of the browser. Deal with it.
When performing a GET
request to an endpoint that only supports POST
, the expected result is a 405
error:
The
405
(Method Not Allowed) status code indicates that the method received in the request-line is known by the origin server but not supported by the target resource. [...]
The browser address bar won't give you much flexibility to test a REST API, once you only can perform GET
requests from it.
To test a REST API, you should use the right tools. They will give you the ability to perform requests using all the HTTP methods, set request headers and other features that are not available in the browser address bar.
Just listing a few:
Upvotes: 0
Reputation: 3763
You are accessing from browser , so default method for browser is GET
try to invoke from and ReST client like POSTMAN where you can change method to POST
Upvotes: 1