tjm1706 tjm1706 - 6 days ago 5
Java Question

Glassfish 4.1.1 + Jersey with JSON -- cannot get it work [SOLVED]

For days I am trying to get Glassfish 4.1.1 and Jersey working together. The tricky part is when there is communication via JSON.

I tried many solutions to get JSON data transfer working. Alas, still not working.

Glassfish and Jersey are 'standards', so I guess there is a standard way to combine these two? What is the correct way of configuring JSON?

Whenever I am starting to work with JSON, I get all kinds of errors.
The last errors I get:

2016-11-25T15:59:52.070+0100|Warning: StandardWrapperValve[Jersey Web Application]: Servlet.service() for servlet Jersey Web Application threw exception
java.lang.ClassNotFoundException: javax.xml.parsers.ParserConfigurationException not found by org.eclipse.persistence.moxy [228]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
etc, etc.


The maven dependencies are:

http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>nl.xyz</groupId>
<artifactId>GlassfishJerseyJson</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>GlassfishJerseyJson</name>

<build>
<finalName>GlassfishJerseyJson</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- <webXml>src\main\webapp\WEB-INF\web.xml</webXml> -->
</configuration>
</plugin>

<plugin>
<groupId>org.glassfish.maven.plugin</groupId>
<artifactId>maven-glassfish-plugin</artifactId>
<version>2.1</version>
<configuration>
<glassfishDirectory>${local.glassfish.home}</glassfishDirectory>
<user>admin</user>
<passwordFile>${local.glassfish.passfile}</passwordFile>
<domain>
<name>domain1</name>
<httpPort>8080</httpPort>
<adminPort>4848</adminPort>
</domain>
<components>
<component>
<name>${project.artifactId}</name>
<artifact>target/${project.build.finalName}.war</artifact>
</component>
</components>
<debug>true</debug>
<terse>false</terse>
<echo>true</echo>
</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>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.6.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.24.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>


The JQuery call is:

function findResource() {
$.ajax({
type: 'GET',
accepts: { json: "application/json, text/javascript" },
dataType: "json",
url: resourceURL,
success: function(data){
alert( "Back from resource: name=" + data.name);
},
error: function(jqXHR, textStatus, errorThrown){
alert('Back from resource error: ' + textStatus + ' - Error: ' + errorThrown + " - Response: " + jqXHR.responseText);
}
});
}


Resource java file:

@Path("myresource")
public class MyResource {
@GET
@Produces( MediaType.APPLICATION_JSON)
public Wine2 getIt() {
Wine2 w = new Wine2();
w.setId( 100);
w.setCountry( "france");
return w;
}
}


Configuration: option 1: web.xml (left the default first line out):

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web 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.coenraets.cellar</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>


Configuration: option 2: MyApplication.java

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;

public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add( MyResource.class);
s.add( WineResource.class);
return s;
}
}

Answer

Yesssss!!!! I found the complete answer.

The solution is by using the original (see above) maven libs ANd to change the following files in the GLASSFISH/modules folder to verion 2.5.0 (or similar):

  • org.eclipse.persistence.moxy.jar
  • org.eclipse.persistence.core.jar

The solution came via [this webpage][1] and similar sites.

In this post and others the org.eclipse.persistence.moxy.jar file in the $Glassfish/modules is replaced with the 2.5.0 version.

Comments