2
votes

I am new to using jersy for implementing rest api

I get the below error, when I call the products service.

 com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/json was not found.

Here is my code:

@GET
@Path("/products")
@Produces(MediaType.APPLICATION_JSON)
public Response productSearch(@QueryParam("name") String name)
{
   List<Product> products = new ArrayList<>();
   products.add(new Product("PRDNAME", "PRDCOST", "PRDMODEL"));
   return Response.ok( products).build();
}

I also tried this:

@GET
@Path("/products")
@Produces(MediaType.APPLICATION_JSON)
public List<Product> productSearch(@QueryParam("name") String name)
{
   List<Product> products = new ArrayList<>();
   products.add(new Product("PRDNAME", "PRDCOST", "PRDMODEL"));
   return products;
}

The below is the setup of my environment:

  • Tomcat 8,
  • Jersey libraries: jersey-bundle-1.19.1,
  • Not using maven

web.xml:

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>org.test.rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

I tried using the below init-param also:

    <init-param>  
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>  
      <param-value>true</param-value>  
    </init-param>

I tried returning GenericEntity, but had the same issue.

return Response.ok().entity(new GenericEntity<List<Product>>(products) {}).build();

I also tried using

  • jersey-core-1.8.jar
  • jersey-json-1.8.jar
  • jersey-server-1.8.jar

instead of jersey-bundle-1.19.1

I don't have any issues when I return string. I understand, I am missing the json media type dependency, but could not figure it out.

This error happens on the tomcat server while the client doesn't receive response obviously.

1
@LeTex I already did try using GenericEntity as mentioned in my question and I find JaxbContentResolver difficult to understand. - ssv
@jarrod-roberson The one you referred, by marking this as duplicate, is not the same. It is about similar error in the client expecting an xml message. My error happens on the server failing to understand JSON & arraylist media types and the client doesn't receive a response. - ssv
all A message body writer for Java type ... , was not found. error messages happen for the exact same reason regardless of the specifics. - user177800
@jarrod-roberson The link provided, fixes code on client side which I cannot(or do not know how to) use it on the server side. Thank You for your Time. --- This might help someone -Though I did not fix the original issue, I managed to get my job done using the below code, for now: @GET @Path("/products") @Produces(MediaType.APPLICATION_JSON) public Response productSearch(@QueryParam("name") String name) { JSONArray resp_jarray = new JSONArray(); // -- fetch data & add objects to the json array-- return Response.status(200).entity(""+resp_jarray ).build(); } - ssv

1 Answers

0
votes

Maybe you forgot to add

@XmlRootElement

annotation on top of model class definition. For your "Product" model, I guess it would be something like

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Product{
....
}

I had a similar exception and this solved it for me.