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

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

问题描述

这个问题已经在这个网站上有一个正确的答案.问题是 问题是针对 Jersey 1.6Jersey 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 ...

然后将适当的 jar 添加到类路径中.仅添加 Jersey jars 是不够的,您还需要添加 jackson.我下载了包含 12 个不同 jar 的 zip.一旦我将所有 12 个 jars 添加到我的类路径中,我终于摆脱了错误并且返回 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 个罐子的 Zip:这是包含 12 个 jar 文件的 zip 文件的链接:jersey-archive-1.17.zip

Download Zip with 12 Jars: Here is a link to the zip file that contains the 12 jar files: 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天全站免登陆