Jersey Inject HK2 2.27的问题:java.lang.ClassCastException

时间:2018-09-12 11:38:39

标签: java rest jersey hk2

我打开这篇文章是因为将Jersey版本从2.25更新到2.27后出现问题。

在更新过程中,Jersey要求我添加jersey-hk2 Maven依赖项。

尽管在泽西岛(Jersey)2.25中,相同的指令也能正常工作,但尝试通过RestClient发送POST请求时却收到ClassCastException,如下所示:

WebTarget webTarget = client.target(resource);
Response response = webTarget.request(MediaType.APPLICATION_JSON)
                             .post(Entity.entity(data, MediaType.APPLICATION_JSON));

这是我收到的整个异常:

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$Builder.method(JerseyInvocation.java:445)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:351)
at com.mypackage.utils.REST.RestClient.executePost(RestClient.java:118)

我指定,正如网上发布的其他问题所建议的那样,我将Jersey组件的所有版本都匹配到2.27。

任何帮助将不胜感激。

非常感谢!

0 个答案:

没有答案