如何使用JAXB使用Jersey 1.17.1生成JSON输出 [英] How to produce JSON output with Jersey 1.17.1 using JAXB

查看:111
本文介绍了如何使用JAXB使用Jersey 1.17.1生成JSON输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此网站上已有对此问题的正确答案。问题是问题适用于Jersey 1.6 泽西岛1.17.1的正确答案隐藏在底部。我想我会为这个答案创建一个正确的问题,这样就可以更容易地为那些正在努力解决这个问题的人找到帮助(就像我一样)。

There is a correct answer to this question already on this site. The problem is that the question is for Jersey 1.6 and the correct answer for Jersey 1.17.1 is buried at the bottom. I figured I'd create a correct question for this answer so that it'd be easier to find help for people struggling with this (like I was).

推荐答案

首先,您需要将其添加到您的web.xml:

First, you need to add this to your web.xml:

<servlet>
    <servlet-name>JerseyServlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>

然后,选择要通过网络序列化的任何类,并将此注释添加到顶部其中:

Then, take any of the classes you want to serialize across the wire and add this annotation to the top of them:

import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class ...

然后添加适当的罐子到类路径。添加泽西罐子是不够的,你还需要添加杰克逊。我下载了包含12个不同罐子的拉链。一旦我将所有12个罐子添加到我的类路径中,我终于摆脱了错误并且很好地返回了JSON。

Then add the appropriate jars to the classpath. It's not enough to add the Jersey jars, you also need to add jackson. I downloaded the zip that includes 12 different jars. Once I added all 12 jars to my classpath I finally got rid of the error and works great returning JSON.

我希望这对某人有帮助。

I hope this helps somebody.

使用12个Jars下载Zip:
这是包含12个jar文件的zip文件的链接: jersey-archive-1.17.zip

对于Maven用户:
将以下内容添加到您的pom.xml中以分别获取12个罐子:

For Maven Users: Add the following to your pom.xml to get the 12 jars individually:

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-jaxrs</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-xc</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jettison</groupId>
        <artifactId>jettison</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>3.1</version>
    </dependency>

这篇关于如何使用JAXB使用Jersey 1.17.1生成JSON输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆