无法实例化提供程序 org.glassfish.jersey.client.JerseyClientBuilder

Posted

技术标签:

【中文标题】无法实例化提供程序 org.glassfish.jersey.client.JerseyClientBuilder【英文标题】:Provider org.glassfish.jersey.client.JerseyClientBuilder could not be instantiated 【发布时间】:2020-01-17 07:56:05 【问题描述】:

我有一个实现 Jersey 客户端的 android 项目。执行该行时出现异常:

Client client = ClientBuilder.newClient();

这是我的 gradle 依赖项:

dependencies 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'androidx.preference:preference:1.1.0'
    implementation 'org.glassfish.jersey.core:jersey-client:2.28'
    implementation 'org.glassfish.jersey.media:jersey-media-jaxb:2.28'
    implementation 'javax.xml.bind:jaxb-api:2.3.1'
    implementation 'com.sun.xml.bind:jaxb-core:2.3.0.1'
    implementation 'com.sun.xml.bind:jaxb-impl:2.3.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

我在 Eclipse 中配置了一个项目,该项目也使用客户端,与 Android 项目具有相同的导入,效果很好。

这是一个例外:

    java.lang.RuntimeException: java.lang.ClassNotFoundException: Provider org.glassfish.jersey.client.JerseyClientBuilder could not be instantiated: java.lang.reflect.InvocationTargetException
        at javax.ws.rs.client.ClientBuilder.newBuilder(ClientBuilder.java:86)
        at javax.ws.rs.client.ClientBuilder.newClient(ClientBuilder.java:97)
        at za.co.ezimax.client.RestExecutor.execute(RestExecutor.java:23)
        at za.co.ezimax.client.RestClient.resetUserPassword(RestClient.java:43)
        at za.co.ezimax.resetpassword.ResetPasswordActivity.resetPassword(ResetPasswordActivity.java:62)
        at za.co.ezimax.resetpassword.ResetPasswordActivity.lambda$onCreate$0$ResetPasswordActivity(ResetPasswordActivity.java:42)
        at za.co.ezimax.resetpassword.-$$Lambda$ResetPasswordActivity$yT5tltpKfEUxjtl2fzaBIET-T-Y.onClick(lambda)
        at android.view.View.performClick(View.java:5106)
        at android.view.View$PerformClick.run(View.java:20329)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5912)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
     Caused by: java.lang.ClassNotFoundException: Provider org.glassfish.jersey.client.JerseyClientBuilder could not be instantiated: java.lang.reflect.InvocationTargetException
        at javax.ws.rs.client.FactoryFinder.newInstance(FactoryFinder.java:94)
        at javax.ws.rs.client.FactoryFinder.find(FactoryFinder.java:185)
        at javax.ws.rs.client.ClientBuilder.newBuilder(ClientBuilder.java:70)
        at javax.ws.rs.client.ClientBuilder.newClient(ClientBuilder.java:97) 
        at za.co.ezimax.client.RestExecutor.execute(RestExecutor.java:23) 
        at za.co.ezimax.client.RestClient.resetUserPassword(RestClient.java:43) 
        at za.co.ezimax.resetpassword.ResetPasswordActivity.resetPassword(ResetPasswordActivity.java:62) 
        at za.co.ezimax.resetpassword.ResetPasswordActivity.lambda$onCreate$0$ResetPasswordActivity(ResetPasswordActivity.java:42) 
        at za.co.ezimax.resetpassword.-$$Lambda$ResetPasswordActivity$yT5tltpKfEUxjtl2fzaBIET-T-Y.onClick(lambda) 
        at android.view.View.performClick(View.java:5106) 
        at android.view.View$PerformClick.run(View.java:20329) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5912) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
        at javax.ws.rs.client.FactoryFinder.newInstance(FactoryFinder.java:90)
        at javax.ws.rs.client.FactoryFinder.find(FactoryFinder.java:185) 
        at javax.ws.rs.client.ClientBuilder.newBuilder(ClientBuilder.java:70) 
        at javax.ws.rs.client.ClientBuilder.newClient(ClientBuilder.java:97) 
        at za.co.ezimax.client.RestExecutor.execute(RestExecutor.java:23) 
        at za.co.ezimax.client.RestClient.resetUserPassword(RestClient.java:43) 
        at za.co.ezimax.resetpassword.ResetPasswordActivity.resetPassword(ResetPasswordActivity.java:62) 
        at za.co.ezimax.resetpassword.ResetPasswordActivity.lambda$onCreate$0$ResetPasswordActivity(ResetPasswordActivity.java:42) 
        at za.co.ezimax.resetpassword.-$$Lambda$ResetPasswordActivity$yT5tltpKfEUxjtl2fzaBIET-T-Y.onClick(lambda) 
        at android.view.View.performClick(View.java:5106) 
        at android.view.View$PerformClick.run(View.java:20329) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5912) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
     Caused by: java.lang.NoClassDefFoundError: org.glassfish.jersey.model.internal.-$$Lambda$_yDVjAJwmPAQhS-G6o52AiDozCY
        at org.glassfish.jersey.model.internal.CommonConfig.<clinit>(CommonConfig.java:68)
        at org.glassfish.jersey.client.ClientConfig$State.<init>(ClientConfig.java:136)
        at org.glassfish.jersey.client.ClientConfig.<init>(ClientConfig.java:491)
        at org.glassfish.jersey.client.JerseyClientBuilder.<init>(JerseyClientBuilder.java:76)
        at java.lang.reflect.Constructor.newInstance(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
        at javax.ws.rs.client.FactoryFinder.newInstance(FactoryFinder.java:90) 
        at javax.ws.rs.client.FactoryFinder.find(FactoryFinder.java:185) 
        at javax.ws.rs.client.ClientBuilder.newBuilder(ClientBuilder.java:70) 
        at javax.ws.rs.client.ClientBuilder.newClient(ClientBuilder.java:97) 
        at za.co.ezimax.client.RestExecutor.execute(RestExecutor.java:23) 
        at za.co.ezimax.client.RestClient.resetUserPassword(RestClient.java:43) 
        at za.co.ezimax.resetpassword.ResetPasswordActivity.resetPassword(ResetPasswordActivity.java:62) 
        at za.co.ezimax.resetpassword.ResetPasswordActivity.lambda$onCreate$0$ResetPasswordActivity(ResetPasswordActivity.java:42) 
        at za.co.ezimax.resetpassword.-$$Lambda$ResetPasswordActivity$yT5tltpKfEUxjtl2fzaBIET-T-Y.onClick(lambda) 
        at android.view.View.performClick(View.java:5106) 
        at android.view.View$PerformClick.run(View.java:20329) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5912) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

【问题讨论】:

【参考方案1】:

This page 可能会为您提供更多帮助。

从您的 resteasy 导入具有特定版本号的 RESTEasy 客户端。

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-client</artifactId>
    <version>3.0.2.Final</version>
</dependency>

这将提供必要的类来构建客户端以发送 RESTful 请求。

如果您仍然遇到任何错误,请使用以下类:

ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://localhost:8080/RESTEasyApplication/user-management/users");
Response response = target.request().get();
String value = response.readEntity(String.class);
System.out.println(value);
response.close();  

它会解决错误。

【讨论】:

以上是关于无法实例化提供程序 org.glassfish.jersey.client.JerseyClientBuilder的主要内容,如果未能解决你的问题,请参考以下文章

flywaydb:无法实例化 jdbc 驱动程序

安装程序无法实例化文件... _KEY_XE.reg。该文件似乎不存在

错误:[$injector:unpr] 未知提供者:$routeProvider,无法实例化模块 myApp,原因是

不可实例化的超类

无法实例化应用程序匕首android

RuntimeException:无法实例化应用程序