27
@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:

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

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

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

petertc
  • 3,607
  • 1
  • 31
  • 36
Mikhail
  • 1,583
  • 5
  • 22
  • 34

6 Answers6

33

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)

Mikhail
  • 1,583
  • 5
  • 22
  • 34
  • 1
    Thanks for this. I kept searching for a much more complex cause. – GuiSim Dec 23 '11 at 20:31
  • 3
    This doesnt work for me.I still get SEVERE: A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/json, was not found. This is while running a test that uses an InMemoryContainer, so I wonder if it has something to do with that. – Puneet Feb 22 '12 at 05:01
  • 1
    I had to change jersey-server to 1.9, jersey-core 1.9 and jersey-json to 1.9 jars. – Milos Pesic Jun 23 '12 at 17:35
  • how to do it for application/xml – Anuj Verma Jun 30 '12 at 16:31
  • 1
    Using Maven - I had to add: com.sun.jersey jersey-json 1.17 – hba Feb 22 '13 at 17:04
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.

    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
Kamran
  • 829
  • 1
  • 10
  • 12
  • 1
    I had everything from the accepted answer, and am using jersey v1.13; this is what solved my issue. Thanks! – Riggy Sep 19 '12 at 18:59
6

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>
11101101b
  • 7,679
  • 2
  • 42
  • 52
  • Can you provide name or link of that zip file containing 12 jar files? – Rahul Shelke Aug 06 '13 at 12:46
  • Sure thing. I just added a link to the zip file. – 11101101b Aug 07 '13 at 14:26
  • it gives me: 400 - Bad Request – Rahul Shelke Aug 08 '13 at 07:58
  • Haha, sorry... Google moved the file. I added another reference to one on a public Maven 2 repository, so you should be good now. – 11101101b Aug 08 '13 at 18:16
  • I think I'm in the same situation as you here. Is there any way to get this working with maven dependencies? – Daniel Kaplan Sep 05 '13 at 20:51
  • 1
    @tieTYT I have added the dependencies individually so you can just drop them into your pom.xml file. – 11101101b Sep 05 '13 at 21:41
  • Thanks a lot. I don't know why there are so many different answers that "worked". Jersey must have changed a lot of things between versions. – Daniel Kaplan Sep 05 '13 at 21:46
  • I created a new question for this that I'd like you to answer here: http://stackoverflow.com/questions/18646747/how-to-produce-json-output-with-jersey-1-17-1-using-jaxb This can help others. I went through a lot of trial and error to get this working before I noticed your answer way at the bottom. – Daniel Kaplan Sep 05 '13 at 22:09
  • Yeah, sometimes it's the answers lower-down on the list that actually end-up working. I added my answer to your other question. I'm glad I was able to help. – 11101101b Sep 06 '13 at 16:24
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

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

Daniel
  • 10,864
  • 22
  • 84
  • 115
Daniel
  • 61
  • 1
  • 2
  • I use jackson-jaxrs 1.9.13 + cxf-bundle-jaxrs 2.7.11. The problem I encounter is that the first request always fail, and successive requests are all success. During debugging, I notice NoClassDefFoundError exception like yours. Adding jackson-xc-1.9.2 solve the problem. I think it's jackson dependency problem – petertc Sep 17 '14 at 02:03
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.

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;
    }
}
Steven
  • 1,564
  • 1
  • 22
  • 34
0

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>
Sultan
  • 1