Search code examples
javajsonjax-rsjersey-2.0restful-architecture

MessageBodyWriter not found for media type=application/json, type=class SalesService.Item, genericType=class SalesService.Item


I'm getting this error: MessageBodyWriter not found for media type=application/json, type=class SalesService.Item, genericType=class SalesService.Item.

What I want to do is let my restful service method return a custom object, in this case object: Item. But im getting this error and in the browser error 500.

Im using JAX-RS version 2.25.1 libraries in netbeans

I know there are allot of questions like this but i could not find any solution that worked exactly for me.

Thanks in advance!

WEB.XML

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 version="3.1">
<display-name>examples.rest.school</display-name>
<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>SalesService</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 REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

Resources Service class:

package SalesService;

import java.util.ArrayList;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import DB.DBConnecter;
import javax.ws.rs.PathParam;
import java.util.List;
import javax.ws.rs.Consumes;


@Path("Sales")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class SalesResources {

private final DBConnecter dbConnector;

public SalesResources() {
    dbConnector = new DBConnecter();
}


@GET
@Path("session/{sessionID}")
@Produces(MediaType.APPLICATION_JSON)
public Item GetItem(@PathParam("sessionID") String sessionID) {
    List<Item> items = new ArrayList<>();
    if(dbConnector.CheckSessionID(sessionID)){
        items = dbConnector.GetItems();
        return items.get(0);
    }
    return new Item();
}
}

Item class:

package SalesService;

import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
public class Item {
private int id;
private String name;
private int quantity;
private double price;
private String type;

public Item(){}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getQuantity() {
    return quantity;
}

public void setQuantity(int quantity) {
    this.quantity = quantity;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public void AddQuantity() {
    this.quantity = quantity + 1;
}

public void RemoveQuantity() {
    this.quantity = quantity - 1;
}

public Item(int id, String name, int quantity, double price, String type) {
    this.id = id;
    this.name = name;
    this.quantity = quantity;
    this.price = price;
    this.type = type;
}
}

StackTrace

04-Mar-2017 21:03:06.273 SEVERE [http-nio-8080-exec-1] org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo MessageBodyWriter not found for media type=application/json, type=class SalesService.Item, genericType=class SalesService.Item.

Solution

  • This was resolved adding

    org.codehaus.jackson.jaxrs
    

    In web.xml like this

    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
    <display-name>examples.rest.school</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>SalesService,org.codehaus.jackson.jaxrs</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 REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    </web-app>
    

    After adding 4 jackson .JAR files which are these

    enter image description here

    And also adding them in libraries folder (reference)