Error while using jersey-client in osgi - No gener

2019-04-21 02:22发布

问题:

I get the following exception when I try to create a WebTarget from a Jersey client.

My code

Client client = ClientBuilder.newClient();
WebTarget baseTarget = client.target("http://127.0.0.1:9000");

Exception stack trace

java.lang.IllegalStateException: No generator was provided
and there is no default generator registered
    at    org.glassfish.hk2.internal.ServiceLocatorFactoryImpl.internalCreate(ServiceLocatorFactoryImpl.java:266)
    at org.glassfish.hk2.internal.ServiceLocatorFactoryImpl.create(ServiceLocatorFactoryImpl.java:247)
    at org.glassfish.jersey.internal.inject.Injections._createLocator(Injections.java:138)
    at org.glassfish.jersey.internal.inject.Injections.createLocator(Injections.java:109)
    at org.glassfish.jersey.internal.RuntimeDelegateImpl.<init>(RuntimeDelegateImpl.java:61)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:374)
    at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:118)
    at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:225)
    at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:135)
    at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:120)
    at javax.ws.rs.core.UriBuilder.newInstance(UriBuilder.java:95)
    at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
    at org.glassfish.jersey.client.JerseyWebTarget.<init>(JerseyWebTarget.java:71)
    at org.glassfish.jersey.client.JerseyClient.target(JerseyClient.java:185)
    at org.glassfish.jersey.client.JerseyClient.target(JerseyClient.java:70)

What could be the issue ?

回答1:

Add the following archives from the Jersey-ext-directory to your classpath:

hk2-api-*.jar

hk2-locator-*.jar

hk2-utils-*.jar

javax-inject-*.jar

jersey-guava-*.jar



回答2:

Register hk2-locator and javassist as bundles on your OSGi framework.

Jersey uses hk2-locator to find services on OSGi.



回答3:

I had this same issue and I could finally solve it by starting my bundle which uses Jersey to publish an endpoint at level 2 in the Felix Framework (OSGi-5.4). I am not sure why, but seems like the HK2-Locator dependencies:

  • hk2-api-2.4.0-b10.jar -> Level 1
  • hk2-locator-2.4.0-b10.jar -> Level 1
  • hk2-utils-2.4.0-b10.jar -> Level 1
  • org.apache.servicemix.bundles.javax-inject-1_2.jar -> Level 1
  • jersey-guava-2.22.1.jar -> Level 1

need to start first before the bundle who uses them:

  • your-bundle-1.0.0.jar -> Level 2

Once you set your bundle to start on this new level then make sure to execute frameworklevel 2 in Felix console to start the bundles under that level. Restart the framework and it will work.