title: Glassfish4 server & jersey2: why does glassfish provide its own jersey2 to application
body: <p>Recently I tried to deploy a Jersey2 application to Glassfish4.1. I had lots of dependency issues and found a lot of ClassCastException.</p>
  
  <p>Later I found the user guide here: <a href="https://jersey.java.net/documentation/latest/modules-and-dependencies.html#servlet-app-glassfish" rel="nofollow">https://jersey.java.net/documentation/latest/modules-and-dependencies.html#servlet-app-glassfish</a></p>
  
  <p>I have to configure pom.xml like:</p>
  
  <p><code>&lt;dependency&gt;
      &lt;groupId&gt;javax.ws.rs&lt;/groupId&gt;
      &lt;artifactId&gt;javax.ws.rs-api&lt;/artifactId&gt;
      &lt;version&gt;2.0.1&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;</code></p>
  
  <p><code>&lt;dependency&gt;
      &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
      &lt;artifactId&gt;jersey-container-servlet&lt;/artifactId&gt;
      &lt;version&gt;2.23.1&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;</code></p>
  
  <blockquote>
    <p>If you are using Glassfish application server, you don't need to package anything with your application, everything is already included. You just need to declare (provided) dependency on JAX-RS API to be able to compile your application.</p>
  </blockquote>
  
  <p>My question is that why Glassfish have to provide jersey2 (JSR implementation) itself for application. Why not just let application to choose the JSR implementation it is using?</p>
  
  <p>I also add <code>glassfish-web.xml</code> under <code>WEB-INF</code>:</p>
  
  <p><code>&lt;glassfish-web-app&gt;
      &lt;class-loader delegate="false" /&gt;
  &lt;/glassfish-web-app&gt;</code></p>
  
  <p>According to the document here (<a href="https://docs.oracle.com/cd/E19798-01/821-1752/beagb/index.html" rel="nofollow">https://docs.oracle.com/cd/E19798-01/821-1752/beagb/index.html</a>):</p>
  
  <p>It will let Glassfish to load classes under <code>WEB-INF/lib/</code> first. But why does Glassfish still use its own jersey version and javax version?</p>
  
  <p>For javax, I guess Glassfish is a java application version and it only support specific JSR implementations. So when I choose JSR implementation in my application, and I have to find out the correct version of Glassfish.</p>
  
  <p>But why is jersey2 so special that glassfish have to provide it. What if I want to use another version of jersey2?</p>
  
  <hr>
  
  <p>Updated:</p>
  
  <p>I ran some more tests. </p>
  
  <p>When I deployed a jersey1 application (jersey1 is included in war file) to glassfish4 and asked glassfish4 to delegate class loader process to its parent, and this application works, and application can handle incoming rest requests. Why? I guess since glassfish does not have jersey1 included, it will load jersey1 from libraries inside war file, and glassfish4 is actually working with jersey1. Does this mean I can override glassfish default behavior to let application to choose the JAX-RS implementation.</p>
  
  <p>And if I replaced jersey1 with jersey2 and still let glassfish4 to load libraries from war first, there was an exception thrown:</p>
  
  <p><code>WebModule[/invoiceLoader]StandardWrapper.Throwable
  java.lang.ClassCastException: Cannot cast org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider to org.glassfish.jersey.server.spi.ComponentProvider
      at java.lang.Class.cast(Class.java:3369)
      at org.glassfish.jersey.internal.ServiceFinder$LazyObjectIterator.hasNext(ServiceFinder.java:713)
      at org.glassfish.jersey.server.ApplicationHandler.getRankedComponentProviders(ApplicationHandler.java:743)
      at org.glassfish.jersey.server.ApplicationHandler.access$600(ApplicationHandler.java:184)
      at org.glassfish.jersey.server.ApplicationHandler$4.get(ApplicationHandler.java:406)
      at org.glassfish.jersey.server.ApplicationHandler$4.get(ApplicationHandler.java:399)
      at org.glassfish.jersey.internal.util.collection.Values$LazyValueImpl.get(Values.java:340)
      at org.glassfish.jersey.server.ApplicationHandler.createApplication(ApplicationHandler.java:366)
      at org.glassfish.jersey.server.ApplicationHandler.&lt;init&gt;(ApplicationHandler.java:342)
      at org.glassfish.jersey.servlet.WebComponent.&lt;init&gt;(WebComponent.java:392)
      at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)</code></p>
  
  <p>How did this exception happen?</p>