I am using a JAX-RS web application with Jersey and Tomcat. Get requests are fine however when I try to post JSON I get an HTTP status 415 - Unsupported Media Type.
Here is my simple HelloWorld.java:
package service;
import javax.ws.rs.*;
@Path("hello")
public class HelloWorld {
@GET
@Produces("text/plain")
public String get() {
return "hello world";
}
@POST
@Consumes("application/json")
public String post(JS input) {
return input.hello;
}
public static class JS {
public String hello;
}
}
Here is the request I try in Postman (with 'application/json' header):
Here is the project layout with libraries:
I am using:
Thanks!
The Jersey distribution doesn't come with JSON/POJO support out the box. You need to add the dependencies/jars.
Add all these
With Maven, below will pull all the above in
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.17</version>
</dependency>
For any future readers not using Jersey 2.17 (and using jars directly instead of Maven), you can go here to find the Jersey version you are using, and see what transitive dependency versions you need. The current version of this Jersey dependency uses Jackson 2.3.2. That's the main thing you need to look out for.