以下是我的POJO课
public class Credentials {
private int cred_id;
private String cred_user_name;
private String cred_password;
private String cred_token;
public Credentials(int cred_id, String cred_user_name,
String cred_password, String cred_token) {
this.cred_id = cred_id;
this.cred_user_name = cred_user_name;
this.cred_password = cred_password;
this.cred_token = cred_token;
}
public int getCred_id() {
return cred_id;
}
public void setCred_id(int cred_id) {
this.cred_id = cred_id;
}
public String getCred_user_name() {
return cred_user_name;
}
public void setCred_user_name(String cred_user_name) {
this.cred_user_name = cred_user_name;
}
public String getCred_password() {
return cred_password;
}
public void setCred_password(String cred_password) {
this.cred_password = cred_password;
}
public String getCred_token() {
return cred_token;
}
public void setCred_token(String cred_token) {
this.cred_token = cred_token;
}
}
以下是我的资源班
public class ValidateUser {
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String validateUser(Credentials credentials) {
System.out.println("Going to validate the user" + credentials);
String username = credentials.getCred_user_name();
String password = credentials.getCred_password();
CredentialsAccessor ca = new CredentialsAccessor();
long count = 0;
count = ca.authenticateUser(username, password);
if (count > 0) {
JSONObject jObject = new JSONObject();
try {
jObject.put("valid", "true");
return jObject.toString();
} catch (JSONException e) {
e.printStackTrace();
return "{'valid':'error'}";
}
} else {
JSONObject jObject = new JSONObject();
try {
jObject.put("valid", "false");
return jObject.toString();
} catch (JSONException e) {
e.printStackTrace();
return "{'valid':'error'}";
}
}
}
}
Please see the maven dependencies here
我确保在RESTClient中,我将内容类型指定为Content-Type:application / json
Please see the RESTClient
下面是我的web.xml
但是即使如此,我仍然收到错误信息“,获取HTTP状态415-不支持的媒体”
你能想象,我要去哪里错了?
最佳答案
我看到你有MOXy。但是MOXy只知道如何处理带有@XmlRootElement
注释的类。这就是415的原因:泽西岛无法找到MessageBodyReader
来处理该类型。
在@XmlRootElement
类的顶部添加Credentials
之后,您将遇到另一个错误,因为MOXy无法将JSON反序列化为您的Credentials
。原因是没有默认的(无参数)构造函数。因此,加上这一点,我认为您应该很好。
关于java - 获取HTTP状态415- Jersey 不支持的媒体类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43140490/