3

I am getting the following exception when using javax HTTP client. Any idea why this might be happening?

java.lang.ClassCastException: Cannot cast org.glassfish.jersey.inject.hk2.Hk2InjectionManagerFactory to org.glassfish.jersey.internal.inject.InjectionManagerFactory
    at java.lang.Class.cast(Class.java:3369)
    at org.glassfish.jersey.internal.ServiceFinder$LazyObjectIterator.hasNext(ServiceFinder.java:714)
    at org.glassfish.jersey.internal.inject.Injections.lookupService(Injections.java:112)
    at org.glassfish.jersey.internal.inject.Injections.lookupInjectionManagerFactory(Injections.java:97)
    at org.glassfish.jersey.internal.inject.Injections.createInjectionManager(Injections.java:68)
    at org.glassfish.jersey.client.ClientConfig$State.initRuntime(ClientConfig.java:432)
    at org.glassfish.jersey.internal.util.collection.Values$LazyValueImpl.get(Values.java:341)
    at org.glassfish.jersey.client.ClientConfig.getRuntime(ClientConfig.java:826)
    at org.glassfish.jersey.client.ClientRequest.getConfiguration(ClientRequest.java:285)
    at org.glassfish.jersey.client.JerseyInvocation.validateHttpMethodAndEntity(JerseyInvocation.java:143)
    at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:112)
    at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:108)
    at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:99)
    at org.glassfish.jersey.client.JerseyInvocation$AsyncInvoker.method(JerseyInvocation.java:706)
    at org.glassfish.jersey.client.JerseyInvocation$AsyncInvoker.get(JerseyInvocation.java:566)

Could it be a classloader related issue? As the code that uses the http client is loaded as a plugin with a separate classloader.

jerrypeng
  • 104
  • 1
  • 5

1 Answers1

0

It certainly is a classloader issue; I have recently faced the same problem with Jersey 2.35. What In understand, Jersey internals in terms of dependency injection architecture has changed (Refer this post: wypieprz answer).

The above occurs due to 'classloader shadowing' (if there is such a term). the contention is between the container class loader (in my case Tomcat JARClassLoader) and web-app class loader (WebbAppClassLoader of Tomcat).

Since class loaded by different class loaders are not considered same. Therefore, in your case (and as was in mine), the 'Hk2InjectionManagerFactory' class and the 'InjectionManagerFactory' class was loaded by different class loaders and therefore the assignment fails with class cast exception.

In my case, the problem was, I had, 'jersey-common' and 'jersey-client' on both web-app and Tomcat common loader (container class path). This caused the problem.

The solution was to move all the Jersey related classes to WEB-INF/lib and the library which depended upon jersey-client also to WEB-INF lib.

In addition, also check your classpath (container class path), if there are any other older versions of jersey related jars.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ironluca
  • 3,402
  • 4
  • 25
  • 32