0

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.

2 Answers 2

1

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

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

2 Comments

Thankx for suggestion sir,but I don't wanna use GET because it is not so secure.
I am not saying to use GET
0

Explaining the 405 error

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:

6.5.5. 405 Method Not Allowed

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. [...]

Picking the right tool

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:

Comments

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.