Search code examples
javajsonjersey-2.0

Jersey moxy-media-module does not support json


Dependencies :-

enter image description hereWhile i am trying to convert POJO to json using moxy-media module i am getting exception.

Java code :-(Pojo)

@XmlRootElement
public class MoxyUser {
public String name;
public int age;

public MoxyUser(){}

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

Resource class :-

@Path("/moxy")
public class MoxyResource {
@GET
@Produces("application/json")
public MoxyUser getMoxyUser(){
    return new MoxyUser("Murugesan",13);
  }
}

Deployment descriptor :-

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>JerseyJsonMoxy</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.json.resources</param-value>
    </init-param>
    <init-param>
        <param-name>jersey-media-moxy</param-name>
        <param-value>org.glassfish.jersey.media</param-value>
    </init-param>
    <init-param>
        <param-name>org.eclipse.persistence.moxy</param-name>
        <param-value>org.eclipse.persistence</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>

Exception :-

java.lang.NoClassDefFoundError: org/eclipse/persistence/internal/queries/ContainerPolicy
java.lang.Class.getDeclaredMethods0(Native Method)

Kindly some one help me to resolve this. i am not using maven.


Solution

  • There's no such thing as that init-param jersey-media-moxy you are trying to use. Wherever you found that, you must have it mistaken for a Maven dependency. The param-name and value you have are actually Maven coordinates for the MOXy dependency. Same with org.eclipse.persistence.moxy. You can completely get rid of all this

    <init-param>
        <param-name>jersey-media-moxy</param-name>
        <param-value>org.glassfish.jersey.media</param-value>
    </init-param>
    <init-param>
        <param-name>org.eclipse.persistence.moxy</param-name>
        <param-value>org.eclipse.persistence</param-value>
    </init-param>
    

    That's not the problem though. The MOXy jar is still dependent on other jars that you do not have. That's why the NoClassDefFound. That means that MOXy is trying to use a class that is not on the classpath.

    I don't know exactly what jars off the top of my head MOXy needs, I normally use Maven and just let Maven pull in the required Jars.

    What I would do instead, is just get rid of the MOXy jar (this is a must for the following to work), then Just use Jackson. Rather than trying to say how, I will just post to an earlier answer that does a good job describing how you can get all the Jackson Jars.

    See:

    If you really must use MOXy, then you need all the following jars

    enter image description here

    Note that you are using a new version of Jersey. The image is an image from an older answer, where the user was using Jersey 2.13. You will want to get the corresponding version of all the eclipse link jars. Please read the link in the "See" above. It describes how to go about finding the correct version that you should look for. It may not be a problem using the same version as in the image, but it's possible you will have problems. I would just make sure to get the right version to go with your version of jersey-moxy