我在@Named @ViewScoped
带有 JBoss-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);
这在独立的客户端应用程序中运行良好(当我删除 时也可以ClientExecutor
,但我需要它来验证 REST 服务)。该 bean 位于 aWAR
内的一个模块中EAR
,resteasy 的依赖层次结构解析为以下内容:
httpclient
orhttpcore
中没有WAR
or EAR
。在 Bean 内部,我得到以下异常:
java.lang.NoClassDefFoundError: org/apache/http/HttpRequestInterceptor
似乎很容易(尽管我想知道 resteasy 包装)并且我添加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)
更新要重现这一点,您不需要 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());
}
}