Search code examples
javatomcatjerseytomcat8

Returning Json From Jersey on Linux Throws Exception


I've developed a Jersey API which returns either XML or JSON (depending on the request header). When deployed on my Windows 2012 server (Tomcat), it works no problem.

When I deploy (after compiling it on Ubuntu) to an Ubuntu machine in AWS (Glassfish), I get the following errors when I request JSON:

The server encountered an internal error that prevented it from fulfilling this request.

exception javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper

root cause org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper

root cause java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper

No errors when I request XML.

I was originally using moxy for JSON serialization, but then started using Jackson.

Any ideas why it would work on one server and not another? Thanks


Solution

  • Finally got this working. First, use Tomcat, not Glassfish.

    Then, add a new dependency:

    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
    </dependency>
    

    I also upgraded Jersey version to 2.26, but I don't think that made the difference. Just adding that for completeness (as it did happen).

    I don't understand the reason it works. I always thought Java ran the same on Windows and Linux. But hey, I'm just a dumb .NET guy.

    Hope this helps others who find themselves in a pit of helplessness on this issue as I did.