0
votes

I'm getting 415- Unsupported media type when seeing a POST request. I checked a lot of solutions given but none seem to work. I set the content type to application/json. But I'm still getting the same issue.

I tried solutions like adding jackson-all.jar, jersey-media-json-jakson jar and adding a no-arg constructor. None seems to work.

Book Resource

package com.jugo.own.rest.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.jugo.own.database.dao.BookDAO;
import com.jugo.own.rest.services.BookRO;

@Path("book")
public class BookResource {

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public String getBook(@PathParam("id") Long id){
        BookDAO book = new BookDAO();
        return book.getBook(id);
    }

    @POST
    @Path("/add")
    @Consumes(MediaType.APPLICATION_JSON)
    public void createBook(BookRO ro) {
        BookDAO book = new BookDAO();
        book.createBooks(ro);
    }

}

BookRO

public class BookRO {

    private String bookName;
    private Long sectionId;
    private Long id;
    private String author;
    private String publication;

    public BookRO(){

    }

    public BookRO(String bookName, Long sectionId, Long id, String author, String publication) {
        this.bookName = bookName;
        this.sectionId = sectionId;
        this.id = id;
        this.author = author;
        this.publication = publication;
    }

    public String getBookName() {
        return bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public Long getSectionId() {
        return sectionId;
    }
    public void setSectionId(Long sectionId) {
        this.sectionId = sectionId;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getPublication() {
        return publication;
    }
    public void setPublication(String publication) {
        this.publication = publication;
    }

}

web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>LibManagement</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Jersey</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.vamsi.own.rest.resources</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

JSON request

{
    "bookName" : "Lord Of The Rings : The Fellowship Of The Ring",
    "sectionId" : 1,
    "id"        : 1,
    "author"    : "J.R.R Tolkein",
    "publication" : "Allen & Unwin"
}

also, below are my jar dependencies.

aopalliance-repackaged-2.3.0-b10.jar
asm-debug-all-5.0.2.jar
com.fasterxml.jackson.core.jar
com.fasterxml.jackson.databind.jar
derby.jar
eclipselink.jar
hk2-api-2.3.0-b10.jar
hk2-locator-2.3.0-b10.jar
hk2-utils-2.3.0-b10.jar
jackson-all-1.9.0.jar
jackson-annotations-2.1.2.jar
jackson-jaxrs-base-2.2.3.jar
jackson-jaxrs-json-provider-2.5.4.jar
jackson-module-jaxb-annotations-2.2.2.jar
javassist-3.18.1-GA.jar
javax.annotation-api-1.2.jar
javax.inject-2.3.0-b10.jar
javax.persistence_2.1.0.v201304241213.jar
javax.persistence.source_2.1.0.v201304241213.jar
javax.servlet-api-3.0.1.jar
javax.ws.rs-api-2.0.1.jar
jaxb-api-2.2.7.jar
jersey-client.jar
jersey-common.jar
jersey-container-servlet-core.jar
jersey-container-servlet.jar
jersey-core-1.9.1.jar
jersey-guava-2.13.jar
jersey-media-json-jackson-2.0-m07-1.jar
jersey-media-multipart-2.13.jar
jersey-server.jar
mimepull.jar
org.eclipse.persistence.jpa.modelgen_2.5.2.v20140319-9ad6abd.jar
org.eclipse.persistence.jpa.modelgen.source_2.5.2.v20140319-9ad6abd.jar
org.eclipse.persistence.jpars_2.5.2.v20140319-9ad6abd.jar
org.eclipse.persistence.jpars.source_2.5.2.v20140319-9ad6abd.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar
persistence-api-1.0.jar
validation-api-1.1.0.Final.jar

Any idea?

1
Are you sure that you send the correct Content-Type header? BTW, no need to use a path /add. Just post into /book.braunpet
Yes, I'm pretty sure that the content-type is set. I'm planning to add more POST calls. But anyway, that shouldn't make a difference right?user3310115
Maybe, a first step would be to clean up the dependencies. From your list it seems like you use different versions of Jersey (1.9.1, 2.0, 2.13). Latest version is 2.25.1. Do you use Maven or Gradle?braunpet
No, should I convert?user3310115
Yes, you should set-up a project using Maven (or Gradle). You might want to have a look at this example.braunpet

1 Answers

0
votes

I had the same problem, in Postman.

My solution was to write my data to [Body] in the [raw] in json format. Like this:

{"message": "UPDATED First Post!"}

And in [Headers] :

[KEY] Content-Type [Value] application/json