14

Im trying to implement Json support in a JavaEE project but had issues with MOXy related exceptions being generated. I read on jersey.java.net that MOXy should be autodiscoverable but it doesnt seem to work when i try.

So to make this easy to pinpoint i just generated a new 'jersey-quickstart-webapp' project and changed MyResource as below (my goal is to use an Application class instead of web.xml but this was the simplest way och pinpointing it. The error occurs no matter what).

@Path("myresource")
public class MyResource {
        @GET
        @Produces(MediaType.APPLICATION_JSON)
        public Response getIt() {
            return Response.status(Response.Status.ACCEPTED).entity(new TestEntity()).build();
        }
    }

TestEntity class (in same package):

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class TestEntity {
    private String content = "SOME CONTENT";

    public String getContent() {
        return content;
    }
}

POM.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>a.b.c</groupId>
    <artifactId>server</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>server</name>

    <build>
        <finalName>server</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <jersey.version>2.22.1</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

I deployed this on a clean Glassfish 4.1.1 using IntelliJ. After this i receive

java.lang.ClassNotFoundException: javax.xml.parsers.ParserConfigurationException not found by org.eclipse.persistence.moxy

So i add beans.xml as below (tried empty as well as i saw indicated in Oracle docs)

<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_1.xsd"
       bean-discovery-mode="all">
</beans>

And i get this error when deploying

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

I tried , for fun, removing the web.xml, changing the dependency jersey-container-servlet-core to jersey-container-servlet and creating an Application class instead (as discussed in ContainerRequestFilter wont run in JavaEE jersey project) but gives the same error. Infact it gives the same error if publishing a clean javaee-api 7.0 dependant project instead of jersey dependencies and gave same error (i suppose glassfish using jersey anyway).

So i guess im missing something here, any kind soul that could fill me in on what? :)

Community
  • 1
  • 1
Base
  • 1,061
  • 1
  • 11
  • 27

3 Answers3

19

Downgraded to Glassfish 4.1.0 and then it worked perfectly. Some issue perhaps with the 4.1.1 release? Will try the nightly as well but it works now.

Base
  • 1,061
  • 1
  • 11
  • 27
19

I managed to get past this issue by updating the Manifest in the org.eclipse.persistence.moxy.jar file that comes with Glassfish 4.1.1, rather than downgrading to Glassfish 4.1.0

Steps I took:

  1. Get the updated Manifest.mf file from this post (attached on 2015-03-26 06:08:50 EDT) https://bugs.eclipse.org/bugs/show_bug.cgi?id=463169

  2. Replace the Manifest.mf file in the org.eclipse.persistence.moxy.jar file with the one you downloaded. The file is found here: {c}:\glassfish4\glassfish\modules\org.eclipse.persistence.moxy.jar

  3. Restart Glassfish

Thanks to those who posted and fixed this issue on bugs.eclipse.org

M. Cory
  • 190
  • 1
  • 4
  • 1
    I just [downloaded eclipse version 2.6.1](http://www.eclipse.org/downloads/download.php?file=/rt/eclipselink/updates/2.6.1.v20150916-55dc7c3.zip) and grabbed `org.eclipse.persistence.moxy.jar` and replaced the one in my `.../glassfish4/glassfish/modules/` folder with the new one. Seems to work well for me. Thanks for the answer! – brain_bacon Mar 11 '16 at 07:35
  • This should be the answer for those stuck with 4.1.1 – kosgeinsky Apr 06 '17 at 23:30
  • in alignment with another answer here: https://stackoverflow.com/a/45762121/500452, downloading the new jar (instead of modifying the old one) is recommended – Yohanes Khosiawan 许先汉 Nov 16 '18 at 11:52
2

Instead of downgrading to 4.1.0, I found a switch to Payara a good bet.

D2TheC
  • 2,203
  • 20
  • 23