Jersey / Atmosphere不能生成JSON

有一个方法返回List,并希望它以JSON的形式发送到客户端。 获取异常“未找到Java类java.util.ArrayList,Java类型java.util.List和MIME媒体类型application / json的消息正文编写器。”

我已经将@XMLRootElement注释添加到我的类声明中,并将POJO映射参数添加到web.xml中,但它不起作用

使用Atmosphere 2.2.3和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>

REST资源:

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

事情类:

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/45571.html

上一篇: Jersey/Atmosphere can't produce JSON

下一篇: How to produce JSON output with Jersey 1.6 using JAXB