为什么我在POST请求中获得不允许的HTTP 405方法?

时间:2016-10-22 09:26:59

标签: java rest post httprequest tomcat8

我正在使用JAX-RS并在我的网络服务中使用以下POST方法:

@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);
        }

    }
}

我有 UserInfo.java ,如下所示: -

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

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

}

我正在使用Chrome的Postman并通过POST请求发送以下JSON

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

我得到一个不允许的HTTP 405方法。我不明白为什么。我在线阅读了一个教程(this one),看了here给出的建议,但没有任何效果。我在Apache Tomcat 8上运行我的Web服务。

1 个答案:

答案 0 :(得分:-3)

你试过了吗?

@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

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

}