Jersey/Atmosphere can't produce JSON

Have a method that returns List and want it to be sent to the client as JSON. Getting an exception "A message body writer for Java class java.util.ArrayList, and Java type java.util.List, and MIME media type application/json was not found."

I've added the @XMLRootElement annotation to my class declaration and added the POJO mapping param to web.xml but it doesn't work

Using Atmosphere 2.2.3 with Atmosphere-Jersey 2.2.3

POM

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.atmosphere</groupId>
            <artifactId>atmosphere-runtime</artifactId>
            <version>2.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.atmosphere</groupId>
            <artifactId>atmosphere-jersey</artifactId>
            <version>2.2.3</version>
        </dependency>
        <dependency>
            <groupId>javax.xml</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.1</version>
        </dependency>
    </dependencies>

RESTFul resource:

package com.foo;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("currencypairs")
@Produces(MediaType.APPLICATION_JSON)
public class CurrencyPairs {


    @GET
    @Path("foo")
    public List<Thing> getPairs()   {

        List<Thing> ret = new ArrayList<Thing>();
        ret.add(new Thing("CAD","USD"));
        ret.add(new Thing("EUR", "USD"));
        ret.add(new Thing("GBP","EUR"));
        return ret;
    }
}

Thing class:

package com.foo;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Thing {

    @XmlElement
    private String a;
    @XmlElement
    private String b;

    public Thing(String a, String b)
    {
        this.setA(a);
        this.setB(b);
    }

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }
}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:j2ee="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    http://java.sun.com/xml/ns/j2ee/web-app_2.5.xsd">

    <description>AtmosphereServlet</description>
    <display-name>AtmosphereServlet</display-name>
    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.providers</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.resources</param-name>
        <param-value>false</param-value>
    </context-param>
    <servlet>
        <description>AtmosphereServlet</description>
        <servlet-name>AtmosphereServlet</servlet-name>
        <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
        <!-- If you want to use Servlet 3.0 -->
        <async-supported>true</async-supported>
        <!-- List of init-param -->
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.foo</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>AtmosphereServlet</servlet-name>
        <!-- Any mapping -->
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

jersey-json-1.6.jar添加到您的依赖项中。

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.6</version>
</dependency>
链接地址: http://www.djcxy.com/p/45572.html

上一篇: RS,以便用户看到Excel的下载弹出窗口?

下一篇: Jersey / Atmosphere不能生成JSON