Jersey: Unsupported JSON Media Type

I want to create a rest api to receive json data and send json data.

My Book class:

@Entity
public class Book {

    @Id
    private String isbn;

    public Book() {

    }
......
}

      

My book:

@Path("/books")
public class BookService {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response buyBook(Book book){
        String isbn = book.getIsbn();
        int number = book.getStock();

        return Response.status(Response.Status.OK).entity("OK").build();
    }
......
}

      

When I try to post this using POSTMAN:

POST /books HTTP/1.1    
Host: localhost:5000    
Content-Type: application/json    
Cache-Control: no-cache    
Postman-Token: 3099cd3a-184a-e442-270a-c118930df2b5    

{    
    "isbn" : "",    
    "title" : "",    
    "author" : "",    
    "stock" : "2"    
}        

      

My API will leave me an answer:

HTTP ERROR 415
Problem accessing /books. Reason:
Unsupported Media Type

      


EDIT: (with pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>wholesalerservice</groupId>
  <version>1.0-SNAPSHOT</version>
  <artifactId>wholesalerservice</artifactId>
  <dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.17</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>9.2.10.v20150310</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-webapp</artifactId>
        <version>9.2.10.v20150310</version>
    </dependency>

    <dependency>
      <groupId>javax.ws.rs</groupId>
      <artifactId>javax.ws.rs-api</artifactId>
      <version>2.0.1</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate.ogm</groupId>
        <artifactId>hibernate-ogm-mongodb</artifactId>
        <version>4.1.3.Final</version>
    </dependency>

    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>eclipselink</artifactId>
      <version>2.5.2</version>
    </dependency>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
      <type>jar</type>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals><goal>copy-dependencies</goal></goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <name>WholesalerService</name>
</project>

      

+3


source to share


2 answers


So, in order to use POJO JSON transformation for entity objects, we need to have MessageBodyReader

(sometimes called providers) to handle the transformation. With Java, Jackson has been the de facto JSON library for a while, and they offer a JAX-RS enabled module which is listed at @Nico Müller

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.5.3</version>
</dependency>

      

The problem is that if we don't configure / register it, the only way it will be detected is through a class scan, which most Jersey apps don't depend on. Instead Jersey offers a wrapper module around these artifacts that wraps it in an auto-detect feature (version 2.9+), so we don't need to configure it

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey2.version}</version> <!-- 2.17 latest as of now -->
</dependency>

      



If you are using Jersey before 2.9, you can always register the function in your application class

@ApplicationPath("/api")
public class AppConfig extends ResourceConfig {
    public AppConfig() {
        packages("...");
        register(JacksonFeature.class);
    }
}

      

Or in web.xml register the function as a provider class

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>
        org.glassfish.jersey.jackson.JacksonFeature
    </param-value>
</init-param>

      

+2


source


Have you included jackson-jaxrs-json-provider in your project?

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.5.3</version>
</dependency>

      



In general: your service is missing the title, author, and stock that you are trying to publish.

+1


source







All Articles