Tho*_*hor 4 java classloader resteasy jboss7.x
我在使用@Named @ViewScopedJBoss-7.1.1-Final 的Bean中使用RestEasy客户端框架,以使用自定义从REST服务检索数据HttpRequestInterceptor:
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.addRequestInterceptor(new PreemptiveAuthInterceptor("test","test"), 0);
ClientExecutor clientExecutor = new ApacheHttpClient4Executor(httpClient); //<---
//The error occurs above, the code below is only for completeness
MyRest rest = ProxyFactory.create(MyRest.class,
"http://localhost:8080/rest",clientExecutor);
Run Code Online (Sandbox Code Playgroud)
这在独立客户端应用程序中工作正常(当我删除ClientExecutor它时也可以,但我需要它来验证REST服务).bean在一个WAR模块里面EAR,resteasy的依赖层次结构解析为:

没有httpclient或httpcore在WAR或EAR.在Bean内部,我得到以下异常:
java.lang.NoClassDefFoundError: org/apache/http/HttpRequestInterceptor
Run Code Online (Sandbox Code Playgroud)
似乎很容易(虽然我想知道重新包装)并且我添加org.apache.httpcomponents:httpclient了编译范围:

不,我得到他跟随例外:
java.lang.LinkageError: loader constraint violation: when resolving method
"org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor.<init>
(Lorg/apache/http/client/HttpClient;)V"
the class loader (instance of org/jboss/modules/ModuleClassLoader)
of the current class, my/TestBean, and
the class loader (instance of org/jboss/modules/ModuleClassLoader)
for resolved class,
org/jboss/resteasy/client/core/executors/ApacheHttpClient4Executor,
have different Class objects for the type org/apache/http/client/HttpClient
used in the signature my.TestBean.init(TestBean.java:65)
Run Code Online (Sandbox Code Playgroud)
更新要重现这一点,您不需要REST接口,实例化时会发生错误ApacheHttpClient4Executor,但您可能需要自定义PreemptiveAuthInterceptor:
public class PreemptiveAuthInterceptor implements HttpRequestInterceptor
{
private String username;
private String password;
public PreemptiveAuthInterceptor(String username, String password)
{
this.username=username;
this.password=password;
}
@Override
public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException
{
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
authState.setAuthScope(org.apache.http.auth.AuthScope.ANY);
authState.setCredentials(new UsernamePasswordCredentials(username,password));
authState.setAuthScheme(new BasicScheme());
}
}
Run Code Online (Sandbox Code Playgroud)
小智 10
为了避免在JBoss上部署应用程序时出现链接错误org.apache.httpcomponents,请在JBoss安装的modules文件夹中配置模块,但避免将HttpComponents中的JAR包含在您的应用程序中:
modules/org/apache/httpcomponents/main.module.xml在此目录中列出这些JAR .Dependencies: org.apache.httpcomponents到MANIFEST.MF您的组件.请注意,步骤1和2中引用的模块已存在.但是,您可能希望包含其他JARS(例如httpclient-cache-x.y.z.jar)或不同版本.
解决开发环境中的类是另一回事.
在类似情况下我遇到了同样的问题.所需的HttpComponents模块已经在我的JBoss(AS 7.1.1)模块目录中.因此,解决问题的所有方法是添加Eustachius所描述的清单条目,在Maven中你可以这样做:
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Dependencies>org.apache.httpcomponents</Dependencies>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
maven-war-plugin的配置相同.
| 归档时间: |
|
| 查看次数: |
7393 次 |
| 最近记录: |