如何使用JAXB使用Jersey 1.6生成JSON輸出

[英]How to produce JSON output with Jersey 1.6 using JAXB


@XmlRootElement
public class Todo {
    private String s = "test";

    public String getS() {
        return s;
    }

    public void setS(String s) {
        this.s = s;
    }

}

and service:

和服務:

@Path("/test")
public class Service {

    @GET
    @Produces({MediaType.APPLICATION_JSON })
    public List<Todo> getAllGadgets() {
        return Arrays.asList(new Todo[] { new Todo() });
    }

}

my web.xml:

我的web . xml。

<servlet>
    <servlet-name>Jersey REST Service</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>com.test</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

All this work if I set MediaType.APPLICATION_XML for Produces annotation. But for JSON I get the following exception:

如果我設置MediaType,這些都可以。APPLICATION_XML Produces注解。但是對於JSON,我有以下的例外:

SEVERE: Mapped exception to response: 500 (Internal Server Error) javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.Arrays$ArrayList, and Java type java.util.List, and MIME media type application/json was not found

SEVERE:映射異常到響應:500(內部服務器錯誤)javax.ws.rs。WebApplicationException:com.sun.jersey.api。MessageException: Java類Java .util的消息體寫入器。數組$ArrayList和Java類型Java .util。未找到MIME媒體類型應用程序/json

I use Jersey 1.6 and, according to the tutorial, JSON format should work with JAXB without any additional programming. What's wrong?

我使用的是Jersey 1.6,根據教程,JSON格式應該與JAXB一起使用,不需要任何其他編程。怎么了?

6 个解决方案

#1


32  

I solved this. All I needed to do was to add jersey-json-1.6.jar library to the project (this is not required part of jersey)

我解決了這個問題。我所需要做的就是添加jercy -json-1.6。項目的jar庫(這不是jersey的必需部分)

#2


14  

Add the following param to the jersey servlet in web.xml file, this is required for the latest 1.x versions of jersey-servlet.

向web中的jersey servlet添加以下參數。xml文件,這是最新的1所需要的。jersey-servlet x的版本。

    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>

#3


6  

I use Google App Engine and have struggled a lot with this also, if you use jersey-bundle-1.17.jar most of the stuff work until you add

我使用谷歌應用引擎,如果你使用jersey- bund1 -1.17,我也很糾結。把大部分的東西都加起來。

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

you will get a lot of strange messages. This is because you miss some jackson jars. If you go to jersey homepage and download the zip and the bundle. Just drop the bundle and from the zip you need to add the 4 jackson jars in your classpath and you should get everything working without any error.

你會收到很多奇怪的信息。這是因為你錯過了一些傑克遜的罐子。如果你去jersey主頁下載zip和bundle。只需刪除這個包,然后從zip中添加4 jackson jar到類路徑中,您應該使所有內容都正常工作,不會出現任何錯誤。

Adding jackson-jaxrs-1.9.2.jar solve this error below

添加jackson-jaxrs-1.9.2。jar解決下面的錯誤

SEVERE: The registered message body writers compatible with the MIME media type are:
application/json ->

Adding jackson-xc-1.9.2.jar solve this warrning below

添加jackson-xc-1.9.2。jar解決下面的問題

java.lang.NoClassDefFoundError: org/codehaus/jackson/xc/JaxbAnnotationIntrospector

. lang。NoClassDefFoundError:org/codehaus/jackson/xc/JaxbAnnotationIntrospector

I hope this helps somebody.

我希望這能幫助別人。

#4


6  

The other answers didn't work for me, but I finally got it to work with JSON.

其他答案對我不起作用,但我最終讓它與JSON一起工作。

I was using the jersey-bundle-1.17.jar (also tried with the asm-3.1.jar and jersey-json-1.17.jar added to classpath and still didn't work). I finally tried downloading 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.

我用的是jercy -bundl -1.17。jar(也嘗試過asm-3.1。jar和jersey - json - 1.17。jar添加到類路徑,但仍然不起作用)。我最終下載了包含12個不同罐子的zip文件。一旦我將所有12個jar添加到我的類路徑中,我最終擺脫了錯誤,並且返回JSON非常有效。

I hope this helps somebody.

我希望這能幫助別人。

Update: Here is a link to the zip file that contains the 12 jar files: jersey-archive-1.17.zip

更新:這里有一個指向zip文件的鏈接,該文件包含12個jar文件:jercy -archive-1.17.zip

Another Update for Maven Users: Add the following to your pom.xml to get the 12 jars individually:

Maven用戶的另一個更新:將以下內容添加到pom中。xml分別獲取12個jar:

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

#5


0  

The message body writer exception listed by the OP will be raised if you don't annotate your POJO (or base POJO) with @XmlRootElement.

如果您不使用@XmlRootElement注釋您的POJO(或基礎POJO),那么OP中列出的消息體寫入器異常將會被提高。

For example:

例如:

@XmlRootElement
public class BaseBean
{
    private Boolean success = Boolean.TRUE;
    private String message;

    /**
     * Empty constructor to satisfy requirements of JAXRS.
     */
    public BaseBean() {}

    /**
     * Returns a simple message to accompany the success/failure.
     * @return
     */
    public String getMessage()
    {
        return message;
    }

    /**
     * Sets the message (if required).
     * @param message
     */
    public void setMessage(String message)
    {
        this.message = message;
    }

    /**
     * Returns a flag indicating whether a request for content was
     * successful.
     * @return
     */
    public Boolean getSuccess()
    {
        return success;
    }

    /**
     * Marks the success of a request for content.
     * @param success
     */
    public void setSuccess(Boolean success)
    {
        this.success = success;
    }
}

#6


0  

Kamran's answer worked for me, just to expand more on the xml:

Kamran的回答對我來說很有用,只是為了擴展xml:

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/05/17/725e6d8dae06e561ceabc443886957f0.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com