我使用RestEasy的客戶端框架的@Named @ViewScoped
豆與JBoss-7.1.1-最後用一個自定義的HttpRequestInterceptor
檢索REST服務的數據:JBoss7:裝載機約束衝突與ReastEasy和HttpClient的定製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服務)。所述豆是在EAR
內部的WAR
模塊,RestEasy的的依賴性的層次結構解析爲以下:
中有WAR
或EAR
沒有httpclient
或httpcore
。在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());
}
}
This已經有一段時間了:https://issues.jboss.org/browse/AS7-2803 – Phyxx