SEVERE: A message body writer for Java class java.util.ArrayList and MIME media type application/json was not found

邮差的信 提交于 2019-11-29 13:11:06

Make sure you don't have multiple Jersey versions in your project. From the list you provided there are modules from 3 different versions (1.2, 1.10, 1.8). For some modules Jersey does a check that the version of a module is the same as the version of the core. If it's not then providers of the module (such as MessageBodyReaders, MessageBodyWriters) are not registered in the runtime. This can be problem in your setup - json vs core (1.8 vs 1.2).

The problem may be how you're trying to return your result. I have seen others write their service-layer code this way too, but Jersey provides a way to do it cleanly and it will support JSON, XML and HTML output which you only need to specify using your @Produces annotation. This is what I do:

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.Response;

@GET
@Produces( MediaType.APPLICATION_JSON )
public Response getEmployees()
{        
    List< Emp >                  matched;
    GenericEntity< List< Emp > > entity;

    matched = myDAO.getAllEmployees();
    entity  = new GenericEntity< List< Emp > >( matched ) { };

    return Response.ok( entity ).build();
}

I'm using the following Jersey libraries:

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

You cannot define the response Xml as List<Emp>, as the JAXB is unable to identify the @XmlRootElement over the java.util.List or java.util.ArrayList class definition.

Ideally, you should have one parent/root element for your collection of Child Elements.

Create one more Class as Employees to contains the Collection of Emp objects as like below and try it.

@GET
@Produces("application/json")    
public Employees getEmployees() {        
    List<Emp> empList = myDAO.getAllEmployees();
    log.info("size   " + empList.size());
    Employees employees = new Employees();
    employees.setEmployeeList(empList);

    return employees;
}

@XmlRootElement(name = "Employees")
public class Employees {

    List<Emp> employeeList;

    //setters and getters goes here
}

@XmlRootElement()
class Emp {
   //fields here
}

Please try this approach, it will work.

Add this to your pom.xml. Solved my problem.

        <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.18.1</version>
    </dependency>
    <dependency>
        <groupId>com.owlike</groupId>
        <artifactId>genson</artifactId>
        <version>0.99</version>
    </dependency>
Ramjiraju Kammila

Specifying @XmlRootElement(name = "yourclass") on the class you want to pass as output. This has solved the problem for me when I get this exception.

I had the same problem.

The thing is it knows how to convert it to xml with the annotation @XmlRootElement but it doesn't know how to convert it to JSON.

So for making it convert everything to JSON with the same annotation of xml(ie @XmlRootElement) we can add

jersey-media-moxy-<whatever version>.jar

or for maven users

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
</dependency>

Also it should have a no argument constructor

Maiga

You have to declare in the servlet container of jersey the param as the following:

'com.sun.jersey.api.json.POJOMappingFeature' like this: 
 <servlet>
    <servlet-name>myServices</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>services</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>

cross check your pojo class may be not done the JAXBinding if not done mark your pojo with @XmlRootElement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!