Search code examples
javajerseycxfglassfish-3

Glassfish 3.1 - CXF and Jersey Clients Deployed in Same War?


I am trying to deploy a war to Glassfish 3.1.1 that makes use of a CXF webservice client library and a Jersey webservice client library. In order to get Glassfish to use CXF instead of Metro as the JAX-WS implementation, I am including a glassfish-web.xml file with the following contents:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE sun-web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Application Server 9.0 Servlet 2.5//EN' 
    'http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd'>

<glassfish-web-app>
    <!-- Need this to tell Glassfish not to load the JAX-WS RI classes so it will 
        use the CXF ones instead -->
    <class-loader delegate="false" />
</glassfish-web-app> 

This has the undesirable effect of causing problems for my Jersey client:

2011 Oct 19 15:04:16,994 MDT [http-thread-pool-80(3)] ERROR my.company.MyServlet - Error testing JerseyClient
java.lang.NoSuchMethodError: com.sun.jersey.core.spi.component.ProviderServices.<init>(Ljava/lang/Class;Lcom/sun/jersey/core/spi/component/ProviderFactory;Ljava/util/Set;Ljava/util/Set;)V
        at com.sun.jersey.api.client.Client.init(Client.java:242)
        at com.sun.jersey.api.client.Client.access$000(Client.java:118)
        at com.sun.jersey.api.client.Client$1.f(Client.java:191)
        at com.sun.jersey.api.client.Client$1.f(Client.java:187)
        at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
        at com.sun.jersey.api.client.Client.<init>(Client.java:187)
        at com.sun.jersey.api.client.Client.<init>(Client.java:170)
        at com.sun.jersey.api.client.Client.create(Client.java:679)
        at my.company.MyJerseyClient.<init>(MyJerseyClient.java:93)

Since the Jersey libraries are included in the war (and not expected to be provided in Glassfish), I don't understand this.

If I don't include the glassfish-web.xml file, the Jersey client works fine, but I get this error on the CXF client:

2011-10-19T15:00:53.993-0600|WARNING|glassfish3.1.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=32;_ThreadName=Thread-2;|StandardWrapperValve[my-servlet]: PWC1406: Servlet.service() for servlet my-servlet threw exception
java.lang.ClassCastException: com.sun.xml.ws.client.sei.SEIStub cannot be cast to org.apache.cxf.frontend.ClientProxy
        at org.apache.cxf.frontend.ClientProxy.getClient(ClientProxy.java:93)
        at my.company.MyCXFClient.<init>(MyCXFClient.java:53)

Is it possible to get these two libraries to deploy (and work) in the same war?


Solution

  • It is not possible. Because the client part depends on the jax-rs api and you can't have two different jax-rs implementations used by one war unless you completely isolate them with separate class loaders, as the jax-rs api itself points to an implementation (and can't point to two implementations) for some factory objects.

    UPDATE: Oops, I see you are using one for SOAP and the other for REST. That may work, but I am not sure. Try to do the following:

    1. Make sure you include jersey-core.jar in your war file as well
    2. Set the JVM property in GF as described in Overriding Jersey with war files chapter of Jersey user guide