I am using the RestEasy Client Framework in @Named @ViewScoped
Bean with JBoss-7.1.1-Final to retrieve data from a REST service using a custom 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);
This works fine in a standalone client application (also good when I ClientExecutor
, but I need it to authenticate the REST service). The bean is located in the WAR
module inside the EAR
, the resteasy dependency hierarchy allows the following:
There is no httpclient
or httpcore
in the WAR
or EAR
. Inside the Bean, the following exception occurs:
java.lang.NoClassDefFoundError: org/apache/http/HttpRequestInterceptor
It seems to be easy (although I'm curious about holiday packaging), and I added org.apache.httpcomponents:httpclient
with a compilation scope:
No, I get the following exception:
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)
Refresh . To reproduce this, you do not need REST interfaces, an error occurs when creating an instance of ApacheHttpClient4Executor
, but you may need a custom 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()); } }