Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey 2.x does not support POJO to json?

While i am trying to convert java object to json object by using jersey.i am having an exception.

Java code :-(POJO)

  package org.jersey.media.type;
  import javax.xml.bind.annotation.XmlRootElement;
  @XmlRootElement
  public class MyJaxbBean {
  public String name;
  public int age;

  public MyJaxbBean() {} // JAXB needs this

  public MyJaxbBean(String name, int age) {
    this.name = name;
    this.age = age;
    }
 }

Resource :-

package org.jersey.media.type;
import javax.ws.rs.GET;
import javax.ws.rs.Path; 
import javax.ws.rs.Produces;
@Path("/media")
public class MediaResources {
@GET
@Produces("application/xml")
public MyJaxbBean getMyBean() {
    return new MyJaxbBean("Agamemnon", 32);
 }
}

Exception:-

Sep 05, 2015 4:20:38 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWr   iterInterceptor aroundWriteTo
    SEVERE: MessageBodyWriter not found for media type=application/json,            type=class org.jersey.media.type.MyJaxbBean, genericType=class     org.jersey.media.type.MyJaxbBean.

Deployment Descriptor:-

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>JerseyAuthentication</display-name>
<servlet>
    <servlet-name>Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>org.jersey.media.type</param-value>
    </init-param>
    <!-- <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.jackson.JacksonFeature,org.glassfish.jersey.jackson.JacksonBinder</param-value>
    </init-param> -->
</servlet>
<servlet-mapping>
    <servlet-name>Application</servlet-name>
    <url-pattern>/service/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>login.html</welcome-file>
</welcome-file-list>
  </web-app>

Dependencies:-enter image description here

I am not using maven ,kindly some one help me to resolve this

like image 445
Murugesan M Avatar asked Sep 05 '15 10:09

Murugesan M


1 Answers

Yeah so the Jersey distribution doesn't come with any JSON/POJO support. You need to find them yourself and add them to your project.

If you want to use the JacksonFeature, then you need to grab all the jars for it. You can specify any class you want in the provider classes param, but if you don't actually have it, nothing will happen. You are not even told by any exceptions.

These are all the jars you need

  • jersey-media-json-jackson-2.17
  • jersey-entity-filtering-2.17
  • jackson-jaxrs-json-provider-2.3.2
  • jackson-core-2.3.2
  • jackson-databind-2.3.2
  • jackson-annotations-2.3.2
  • jackson-jaxrs-base-2.3.2
  • jackson-module-jaxb-annotations-2.3.2

Note that these are all the jars that are used for Jersey 2.17. If you are using a newer version, then you should go here, and select the version of Jersey you are using. You can download it. Then search for the entity-filtering jar with the same version.

All the other Jackson jars are of the same version, but you should see which version of Jackson the jersey-media-json-jackson uses. When you click the Jersey version, you should be able to scroll down to see which Jackson version it uses. Then just start searching for all the above Jackson jars for that version.

For instance, if you select the latest Jersey 2.21 version of the jersey-media-json-jackson, you can scroll down and see that it uses Jackson 2.5.4. So you should search for all the above Jackson jars in 2.5.4 instead of the 2.3.2 as shown above.


Note for Maven users, all you need is

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey2.version}</version>
</dependency>

This dependency will pull in all the jars you see listed above.

like image 174
Paul Samsotha Avatar answered Nov 08 '22 03:11

Paul Samsotha