Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MOXy exceptions in JavaEE Jersey 2.0 project

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? :)

like image 241
Base Avatar asked Oct 24 '15 15:10

Base


1 Answers

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.

like image 50
Base Avatar answered Nov 08 '22 17:11

Base