Java – Unsupported Media Type – HTTP Status 415

androidhttp-status-code-415javanetbeansrest

I have a problem with my web service.
GET requests are executed well and correct but the post request is getting the HTTP Status 415.

The project I am working on is a JAX-RS RESTful API that will need to communicate with an Android mobile application. I can receive the information from the GET statement.

This is the code of my LoginFormat object:

@XmlRootElement
public class LoginFormat {

    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Here you can see the POST sample of my CoCreationService class:

@Path("/User")
public class CoCreationService {
    @POST
    @Path("/testLogin") 
    @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    public Response parseTerm(LoginFormat login) {  

        return Response.status(200).entity(login.getUsername() + login.getPassword()).build();  
    }
}

I tried so much that I am confused from it.

I was testing the web service wit curl:

curl -i -X POST -H 'Content-Type: application/json' -d '{"username": "testuser", "password": "test"}' http://localhost:8080/CoCreationService/api/User/testLogin

Is there some setting that needs to be said or did I make a crucial mistake?

PS: I am working with NetBeans.

Edit: A POST with text/plain works!

@POST
@Path("/testPost")
@Consumes("text/plain")
public Response postClichedMessage(String message) {       
    return Response.status(200).entity(message).build();
}

Best Answer

There is nothing wrong with the code. The only thing you need to do is to include proper library in your classpath which knows how to unmarshall json string. If using Maven you can simply add another dependency:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>${jersey.version}</version>
</dependency>

As long as you are using Netbeans you can see in the AS log something like this:

SEVERE: A message body reader for Java class LoginFormat, and 
Java type class LoginFormat, and MIME media type application/json 
was not found.

The registered message body readers compatible with the MIME media type are: