简体   繁体   中英

Why do I get an HTTP 405 Method Not Allowed on a POST request?

I'm using JAX-RS and have the following POST method in my web service:

@Path("create")
public class Create {

    public Create() {

    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public PostBundle createNewUser(final UserCredentials userCredentials) {

        final String name = userCredentials.getName();
        final String mobile = userCredentials.getMobile();
        final String password = userCredentials.getPassword();

        if  (mobile != null && password != null) {
            PostManager postManager = new PostManager();
            Response response = postManager.createUserByMobile(name, mobile, password);

            if  (response.getStatus() == Status.ALREADY_EXISTS)
                return new PostBundle("409","Conflict",null);
            else if (response.getStatus() == Status.SUCCESS) {
                return postManager.getPostBundleByMobile(name, mobile, password);
            } else
                return new PostBundle("500",response.getMessage(),null);

        } else {
            return new PostBundle("400", "BAD REQUEST",null);
        }

    }
}

And I have the UserInfo.java as follows: -

public class UserCredentials {

    private String name;
    private String email;
    private String mobile;
    private String password;

    public UserCredentials() {

        name = null;
        email = null;
        mobile = null;
        password = null;

    }

//Usual getters and setters

............

}

I'm using Chrome's Postman and sending the following JSON with a POST request

{
    "name"      :   "Arijit Banerjee",
    "email"     :   "",
    "mobile"    :   "0123456789",
    "password"  :   "65656565"
}

I get an HTTP 405 Method Not Allowed. I don't understand why. I followed a tutorial online ( this one ), had a look at the suggestion given here , but nothing worked. I'm running my Web service on Apache Tomcat 8.

Have you tried:

@XmlRootElement
public class UserCredentials {

    private String name;
    private String email;
    private String mobile;
    private String password;

    public UserCredentials() {

        name = null;
        email = null;
        mobile = null;
        password = null;

    }

//Usual getters and setters

............

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM