56

I am new to using JAX-RS and wrote a sample application that outputs a json object. but I am getting an exception. Here is my code:

@Path("/hello")
public class HelloWorldService {

    @GET
    @Path("/query/{artist_id}")
    @Produces("application/json")
    public Data getMsg(@PathParam("artist_id") int artist_id,
                            @QueryParam("from") int from,
                            @QueryParam("to") int to) {
        Data d=new Data();
        d.setName("Mateen");
        d.setRoll(77);
        return d;

    }

}

My data is simply a POJO class:

@XmlRootElement
public class Data {
    private int roll;
    private String name;
    public int getRoll() {
        return roll;
    }
    public void setRoll(int roll) {
        this.roll = roll;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

I get an exception:

javax.ws.rs.WebApplicationException: 
    com.sun.jersey.api.MessageException: 
    A message body writer for Java class com.abc.data.Data, 
    and Java type class com.abc.data.Data, 
    and MIME media type application/json was not found

What am i doing wrong ?

Simulant
  • 19,190
  • 8
  • 63
  • 98
user1730789
  • 5,157
  • 8
  • 36
  • 57
  • [please check this post, you need to register the ability for Jersey to output JSON][1] [1]: http://stackoverflow.com/questions/5161466/how-do-i-use-the-jersey-json-pojo-support – chrislhardin Oct 28 '12 at 11:27
  • possible duplicate of [How to produce JSON output with Jersey 1.6 using JAXB](http://stackoverflow.com/questions/6027097/how-to-produce-json-output-with-jersey-1-6-using-jaxb) – rds May 02 '13 at 20:53
  • Why you need to provide @XmlRootElement on the bean? – deadend May 03 '17 at 07:24

6 Answers6

102

I finally found my answer. I added

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.8</version>
</dependency>

to my pom.xml file. Then I added

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

to my web.xml file, and everything works fine. No change was required to my code above.

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
user1730789
  • 5,157
  • 8
  • 36
  • 57
  • 3
    Thank you, it works for me. Also, found the same on official web-page of Jersey https://jersey.java.net/documentation/1.18/json.html (Sections 5.1+5.2 at least) – Yauhen Nov 09 '14 at 18:33
  • I would like to know how I can write similar lines in the Gradle-built, "build.gradle" file with same function as the pom.xml refered above? My project is built on Gradle. – jsh6303 Aug 05 '15 at 19:38
  • Recommend using `runtime`, because the code isn't compiled against the `jersey-json` package - just used at runtime. – AlikElzin-kilaka Jun 07 '16 at 05:47
  • @user1730789 Issue exists even i added jersey-json-1.9.1.jar. i have opened jar and no class like com.sun.jersey.api.json.POJOMappingFeature – deadend May 03 '17 at 07:35
16

I added following jars and it worked for me

  • jackson-core-asl-1.9.2.jar
  • jackson-mapper-asl-1.9.2.jar
  • jackson-xc-1.9.2.jar
  • jackson-jaxrs-1.9.2.jar
Ser Yoga
  • 466
  • 1
  • 6
  • 18
14

Just a small addition. In case if you do not use the web.xml descriptor file, you may enable the POJOMappingFeatire programatically as shown below

...
final ResourceConfig rc = new PackagesResourceConfig("com.test.resources");
    final Map<String, Object> config = new HashMap<String, Object>();
    config.put("com.sun.jersey.api.json.POJOMappingFeature", true);
    rc.setPropertiesAndFeatures(config);
...
Cyril Deba
  • 1,200
  • 2
  • 16
  • 30
  • http://stackoverflow.com/questions/17342218/getting-error-a-message-body-writer-for-java-class-java-util-arraylist-listjava ..can you solve this ?? – user2416728 Jun 27 '13 at 11:58
  • 3
    Please indicate where exactly do I add the above lines of code? – biliboc Mar 25 '15 at 05:50
5

Maven User: You just need the following 2 dependencies.

<!-- For Jersey --> 
<dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.15</version>
</dependency>

<!-- For JSON --> 
<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.15</version>    
</dependency>

XML output is supported by default. So no dependency needed for it.

Sorter
  • 9,704
  • 6
  • 64
  • 74
  • 2
    FYI, this is only valid for using `web.xml` configuration. If you want to use `Applitation/ResourceConfig` subclass for your configuration, then you need the `jersey-container-servlet` instead of the `core`. The former, has the `ServletContainerInitializer`, which initializes the Jersey servlet, without the need for web.xml. If you are in a 2.5 servlet container, then you will need the `web.xml` though. No way around it. But most people should be in a 3.x containers anyway – Paul Samsotha Apr 05 '15 at 00:37
3

Working with Jersey 1.8, I was getting the same error while creating a JSON object and hitting a REST API from client side.

If you are facing issue at server side then, make sure that you have correctly included jersey-json.jar in classpath and mapped correct init-param in web.xml as mentioned in other answers.

For Client Side

As per Jersey 1.8 documentation, the following snippet shows how to use the POJO JSON mapping feature on the client side:

 ClientConfig clientConfig = new DefaultClientConfig();
 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,Boolean.TRUE);
 Client client = Client.create(clientConfig);
akshitmahajan
  • 668
  • 1
  • 9
  • 17
0

I had the same issue: A message body writer for Java type, class java.lang.String, and MIME media type, application/json, was not found

The problem was that the class javax.ws.rs.ext.MessageBodyWriter was taken from

<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.22.2</version>
    </dependency>

Was colliding with the same class from:

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.19.1</version>
    </dependency>
Nir Sivan
  • 75
  • 5