gpt4 book ai didi

java - JBoss7 : loader constraint violation with ReastEasy and httpclient with custom HttpRequestInterceptor

转载 作者:搜寻专家 更新时间:2023-11-01 01:03:15 26 4
gpt4 key购买 nike

我在带有 JBoss-7.1.1-Final 的 @Named @ViewScoped Bean 中使用 RestEasy 客户端框架,通过自定义 HttpRequestInterceptor 从 REST 服务检索数据>:

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);

这在独立的客户端应用程序中工作正常(当我删除 ClientExecutor 时也可以,但我需要它来验证 REST 服务)。该 bean 位于 EAR 内的 WAR 模块中,resteasy 的依赖层次结构解析为以下内容:

Resteasy dependency

WAREAR 中没有httpclienthttpcore。在 Bean 内部,我得到以下异常:

java.lang.NoClassDefFoundError: org/apache/http/HttpRequestInterceptor

似乎很简单(尽管我对 resteasy 包装感到疑惑)并且我添加了 org.apache.httpcomponents:httpclientcompile 范围:

enter image description here

不,我得到他以下异常:

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)

更新 要重现此问题,您不需要 REST 接口(interface),错误发生在实例化 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());

}
}

最佳答案

为避免在 JBoss 上部署应用程序时出现链接错误,请在 JBoss 安装的模块文件夹中配置模块 org.apache.httpcomponents,但避免包含 JAR来自 HttpComponents 和你的应用程序:

  1. 将来自 HttpComponents 的所需 JAR 放在 modules/org/apache/httpcomponents/main 中。
  2. 在此目录的 module.xml 中列出这些 JAR。
  3. Dependencies: org.apache.httpcomponents 添加到组件的 MANIFEST.MF

请注意,步骤 1 和 2 中引用的模块已经存在。但是,您可能希望包括其他 JARS(例如 httpclient-cache-x.y.z.jar)或不同版本。

在您的开发环境中解析类是另一回事。

关于java - JBoss7 : loader constraint violation with ReastEasy and httpclient with custom HttpRequestInterceptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10598889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com