Message body writer for Java class not found

I am new to using JAX-RS and have written an example application that outputs a json object. but I get 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 just 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?

+53
java rest jax-rs
Oct 28
source share
7 answers

I finally found my answer. I added

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

to the 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. There were no changes to my code.

+94
Oct 28 '12 at 11:48
source share

I added the following jars and it worked for me

  • JACKSON-core-ASL-1.9.2.jar
  • JACKSON-Mapper-ASL-1.9.2.jar
  • JACKSON-HS-1.9.2.jar
  • JACKSON-jaxrs-1.9.2.jar
+16
Jul 12 '13 at 8:23
source share

Just a small addition. If you are not using the web.xml descriptor file, you can enable POJOMappingFeatire programmatically, 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); ... 
+12
Apr 05 '13 at
source share

Maven user: you only need two 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. Therefore, this does not require any dependence.

+5
Feb 12 '15 at 21:58
source share

When working with Jersey 1.8, I got the same error when creating a JSON object and hit the REST API from the client side.

If you run into a server-side issue, make sure you include jersey-json.jar in the classpath correctly and map the correct init-param in web.xml, as mentioned in other answers.

For the client side

According to the Jersey 1.8 documentation, the following snippet shows how to use the client side POJO JSON mapping function:

  ClientConfig clientConfig = new DefaultClientConfig(); clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,Boolean.TRUE); Client client = Client.create(clientConfig); 
+2
May 19 '16 at 20:42
source share

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

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

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

In contact with the same class:

  <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.19.1</version> </dependency> 
0
Apr 18 '16 at 10:53 on
source share

I had the same problem, but my error was that I imported the wrong library named javax.websocket.sever to annotate the PathParam in the REST class. I hope this helps you

0
Jan 22 '19 at 13:19
source share



All Articles