How to get Glassfish 4.1.1 and Jersey to work with JSON?
<p>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. </p>

<p>I tried many solutions to get JSON data transfer working. Alas, still not working. </p>

<p>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? </p>

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

<pre><code>     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. 
</code></pre>

<p>The maven dependencies are: </p>

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

<pre><code>&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

&lt;groupId&gt;nl.xyz&lt;/groupId&gt;
&lt;artifactId&gt;GlassfishJerseyJson&lt;/artifactId&gt;
&lt;packaging&gt;war&lt;/packaging&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;GlassfishJerseyJson&lt;/name&gt;

&lt;build&gt;
    &lt;finalName&gt;GlassfishJerseyJson&lt;/finalName&gt;
    &lt;plugins&gt;
            &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.1&lt;/version&gt;
            &lt;configuration&gt;
                &lt;source&gt;1.7&lt;/source&gt;
                &lt;target&gt;1.7&lt;/target&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;

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

        &lt;plugin&gt;
            &lt;groupId&gt;org.glassfish.maven.plugin&lt;/groupId&gt;
            &lt;artifactId&gt;maven-glassfish-plugin&lt;/artifactId&gt;
            &lt;version&gt;2.1&lt;/version&gt;
            &lt;configuration&gt;
                &lt;glassfishDirectory&gt;${local.glassfish.home}&lt;/glassfishDirectory&gt;
                &lt;user&gt;admin&lt;/user&gt;
                &lt;passwordFile&gt;${local.glassfish.passfile}&lt;/passwordFile&gt;
                &lt;domain&gt;
                    &lt;name&gt;domain1&lt;/name&gt;
                    &lt;httpPort&gt;8080&lt;/httpPort&gt;
                    &lt;adminPort&gt;4848&lt;/adminPort&gt;
                &lt;/domain&gt;
                &lt;components&gt;
                    &lt;component&gt;
                        &lt;name&gt;${project.artifactId}&lt;/name&gt;
                        &lt;artifact&gt;target/${project.build.finalName}.war&lt;/artifact&gt;
                    &lt;/component&gt;
                &lt;/components&gt;
                &lt;debug&gt;true&lt;/debug&gt;
                &lt;terse&gt;false&lt;/terse&gt;
                &lt;echo&gt;true&lt;/echo&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

    &lt;dependencyManagement&gt;
        &lt;dependencies&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.glassfish.jersey&lt;/groupId&gt;
                &lt;artifactId&gt;jersey-bom&lt;/artifactId&gt;
                &lt;version&gt;${jersey.version}&lt;/version&gt;
                &lt;type&gt;pom&lt;/type&gt;
                &lt;scope&gt;import&lt;/scope&gt;
            &lt;/dependency&gt;
        &lt;/dependencies&gt;
    &lt;/dependencyManagement&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;javax&lt;/groupId&gt; 
            &lt;artifactId&gt;javaee-web-api&lt;/artifactId&gt; 
            &lt;version&gt;7.0&lt;/version&gt; 
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-container-servlet-core&lt;/artifactId&gt; 
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.fasterxml.jackson.jaxrs&lt;/groupId&gt;
            &lt;artifactId&gt;jackson-jaxrs-json-provider&lt;/artifactId&gt;
            &lt;version&gt;2.6.3&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
            &lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
            &lt;version&gt;2.6.3&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-media-json-jackson&lt;/artifactId&gt;
            &lt;version&gt;2.5.1&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;log4j&lt;/groupId&gt;
            &lt;artifactId&gt;log4j&lt;/artifactId&gt;
            &lt;version&gt;1.2.17&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
    &lt;properties&gt;
        &lt;jersey.version&gt;2.24.1&lt;/jersey.version&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;/properties&gt;
&lt;/project&gt;
</code></pre>

<p>The JQuery call is: </p>

<pre><code>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);
        }
    });
}
</code></pre>

<p>Resource java file: </p>

<pre><code>@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;
     }
} 
</code></pre>

<p>Configuration: option 1: web.xml (left the default first line out): </p>

<pre><code>&lt;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"&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;Jersey Web Application&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
        &lt;init-param&gt;
            &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
            &lt;param-value&gt;org.coenraets.cellar&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;Jersey Web Application&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/webapi/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
&lt;/web-app&gt;
</code></pre>

<p>Configuration: option 2: MyApplication.java</p>

<pre><code>import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;

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