SEVERE:找不到Java类java.util.ArrayList和MIME媒体类型application / json的消息正文编写器

时间:2013-11-21 09:02:00

标签: java rest jersey oc4j jdk1.5

我正在测试RESTful服务,当我执行时我得到异常,虽然我的类路径(WEB-INF / lib)中有以下jar,但我没有使用Maven,我的JDK版本是1.5。关于这个问题的其他问题无助于解决问题。

代码段

@GET
@Produces("application/json")    
//@Produces({MediaType.APPLICATION_JSON}) tried this, didn't work either
public List<Emp> getEmployees() {        
    List<Emp> empList = myDAO.getAllEmployees();
    log.info("size   " + empList.size());
    return empList;
}

@XmlRootElement
public class Emp {
......

的web.xml

<servlet>
    <servlet-name>Jersey Web Application</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>test.employees</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

 <servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

罐子列表

jersey-server-1.2.jar
jersey-core-1.2.jar
jsr311-api-1.1.jar
asm-3.1.jar
jaxb-api-2.0.jar
jaxb-impl-2.0.jar
jackson-xc-1.2.0.jar
jackson-jaxrs-1.2.0.jar
jackson-mapper-asl-1.2.0.jar
jackson-core-asl-1.2.0.jar
jettison-1.2.jar
jersey-client-1.2.jar
jersey-servlet-1.10.jar
jersey-json-1.8.jar

异常堆栈

 SEVERE: A message body writer for Java class java.util.ArrayList,
 and Java type java.util.List<test.Emp>, 
 and MIME media type application/json was not found
Nov 21, 2013 11:47:26 AM com.sun.jersey.spi.container.ContainerResponse traceException
SEVERE: Mapped exception to response: 500 (Internal Server Error)

javax.ws.rs.WebApplicationException
    at javax.ws.rs.WebApplicationException.<init>(WebApplicationException.java:97)
    at javax.ws.rs.WebApplicationException.<init>(WebApplicationException.java:55)
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:267)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1035)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:947)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:939)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:399)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:478)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:663)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:719)
    at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:376)
    at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:870)
    at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:451)
    at com.evermind.server.http.HttpRequestHandler.serveOneRequest(HttpRequestHandler.java:218)
    at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:119)
    at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:112)
    at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260)
    at oracle.oc4j.network.ServerSocketAcceptHandler.procClientSocket(ServerSocketAcceptHandler.java:230)
    at oracle.oc4j.network.ServerSocketAcceptHandler.access$800(ServerSocketAcceptHandler.java:33)
    at oracle.oc4j.network.ServerSocketAcceptHandler$AcceptHandlerHorse.run(ServerSocketAcceptHandler.java:831)
    at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303)
    at java.lang.Thread.run(Thread.java:595)

如何解决此问题?

9 个答案:

答案 0 :(得分:15)

问题可能在于您是如何尝试返回结果的。我已经看到其他人也用这种方式编写服务层代码,但Jersey提供了一种干净利落的方式,它将支持JSON,XML和HTML输出,您只需使用 @Produces 指定注解。这就是我的工作:

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();
}

我正在使用以下泽西岛图书馆:

  • 球衣核-1.8.jar
  • Jersey的JSON-1.8.jar
  • 球衣 - 服务器 - 1.8.jar

答案 1 :(得分:8)

您无法将回复Xml定义为List<Emp>,因为JAXB无法识别@XmlRootElementjava.util.List上的java.util.ArrayList课程定义。

理想情况下,您的子元素集合应该有一个父/根元素。

再创建一个类Employees以包含Emp对象的集合,如下所示,并尝试使用。

@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
}

请尝试这种方法,它会起作用。

答案 2 :(得分:7)

确保项目中没有多个Jersey版本。从您提供的列表中有3个不同版本(1.2,1.10,1.8)的模块。对于某些模块,Jersey会检查模块的版本是否与核心版本相同。如果不是,则模块的提供者(例如MessageBodyReaders,MessageBodyWriters)未在运行时中注册。这可能是你的设置中的问题 - json vs core(1.8 vs 1.2)。

答案 3 :(得分:6)

将其添加到您的pom.xml中。解决了我的问题。

        <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>

答案 4 :(得分:3)

在要作为输出传递的类上指定@XmlRootElement(name = "yourclass")。当我得到这个例外时,这解决了我的问题。

答案 5 :(得分:1)

我遇到了同样的问题。

问题是它知道如何使用注释@XmlRootElement将其转换为xml,但它不知道如何将其转换为JSON。

因此,为了使所有内容都转换为具有相同xml注释的JSON(即@XmlRootElement),我们可以添加

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

或为maven用户

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

它也应该有一个无参数构造函数

答案 6 :(得分:1)

您必须在球衣的servlet container中声明以下内容:

'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>

答案 7 :(得分:1)

交叉检查您的pojo类是否未完成JAXBinding,如果未完成,请使用@XmlRootElement标记您的pojo

答案 8 :(得分:1)

我有同样的问题。然后我解决了。

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>aaa</groupId>
    <artifactId>aaa</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>

    </build>
    <dependencies>
        <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>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.7.3</version>
        </dependency>
        <dependency>
            <groupId>jakarta.ws.rs</groupId>
            <artifactId>jakarta.ws.rs-api</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.19.4</version>
        </dependency>
    </dependencies>
</project>

我的方法在这里。

public static void postData() {
    Staff staff = new Staff();
    staff.setStaffname("Celal");                staff.setStafflastname("Aygar");
    staff.setEmail("celal.aygar@gmail.com");    staff.setGender("Male");
    staff.setCity("DENIZLI");
    String uri = "http://localhost:8185/staff";
    ClientConfig clientConfig = new DefaultClientConfig();
    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
    Client client = Client.create(clientConfig);
    WebResource resource = client.resource(uri);
    try {
        ClientResponse response = resource.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)
                .post(ClientResponse.class, staff);
        if (response.getStatus() == 200) System.out.println("Staff detail : " + response.getEntity(Staff.class));
    } catch (Exception e) { System.out.println("Exception is:" + e.getMessage()); }
}

我的实体在这里。

@XmlRootElement()
public class Staff {
    private Long staffid;

    private String staffname;
    private String stafflastname;
    private String gender;
    private String email;
    private String city;

    //getter setter toString

}