Я использую RestEasy Client Framework в @Named @ViewScoped
Bean с JBoss-7.1.1-Final для извлечения данных из службы 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). Компонент находится в модуле WAR
внутри модуля EAR
, иерархия зависимостей resteasy сводится к следующему:
В WAR
или EAR
нет httpclient
или httpcore
. Внутри Bean я получаю следующее исключение:
java.lang.NoClassDefFoundError: org/apache/http/HttpRequestInterceptor
Кажется, это легко (хотя мне интересно, как упаковать resteasy), и я добавил org.apache.httpcomponents:httpclient
с областью действия compile:
Нет, я получаю следующее исключение:
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());
}
}