How to produce JSON output with Jersey 1.6 using JAXB

后端 未结 6 836
忘了有多久
忘了有多久 2020-11-30 07:41
@XmlRootElement
public class Todo {
    private String s = \"test\";

    public String getS() {
        return s;
    }

    public void setS(String s) {
        th         


        
相关标签:
6条回答
  • 2020-11-30 07:54

    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)

    0 讨论(0)
  • 2020-11-30 07:55

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

        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
    
    0 讨论(0)
  • 2020-11-30 07:58

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

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 07:59

    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

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

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

    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

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

    I hope this helps somebody.

    0 讨论(0)
  • 2020-11-30 08:11

    The other answers didn't work for me, but I finally got it to work with 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.

    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

    Another Update 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>
    
    0 讨论(0)
  • 2020-11-30 08:17

    Kamran's answer worked for me, just to expand more on the 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>
    
    0 讨论(0)
提交回复
热议问题