Balaji Vignesh
Balaji Vignesh

Reputation: 456

Getting HTTP Status 415 - Unsupported Media Type with Jersey

Below is my POJO class

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

}

Below is my resource class

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

I ensured that in the RESTClient i give the content type as Content-Type:application/json

Please see the RESTClient

Below is my web.xml

But even then i get the error 'Getting HTTP Status 415 - Unsupported Media'

Can you image, where am I going wrong?

Upvotes: 3

Views: 684

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209004

I See you have MOXy. But MOXy only knows how to handle classes annotated with @XmlRootElement. That's the reason for the 415: Jersey can't find a MessageBodyReader to handle the type.

After you add the @XmlRootElement on top of the Credentials class, you're going to get another error because MOXy will not be able to actually deserialize the JSON to your Credentials. The reason is that there's no default (no-arg) constructor. So add that, and I'm thinking you should be good.

Upvotes: 2

Related Questions