我在使用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位于WAR
内的EAR
模块中,resteasy的依赖层次结构解析为:
httpclient
或httpcore
中没有WAR
或EAR
。在Bean内部,我得到以下异常:
java.lang.NoClassDefFoundError: org/apache/http/HttpRequestInterceptor
似乎很容易(虽然我想知道重新包装)并且我在编译范围内添加了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());
}
}
答案 0 :(得分:10)
为了避免在 请注意,步骤1和2中提到的模块已存在。但是,您可能希望包含其他JARS(例如 解决开发环境中的类是另一回事。org.apache.httpcomponents
,但避免将HttpComponents中的JAR包含在您的应用程序中: / p>
modules/org/apache/httpcomponents/main
。module.xml
中列出这些JAR。Dependencies: org.apache.httpcomponents
添加到您组件的MANIFEST.MF
。httpclient-cache-x.y.z.jar
)或不同版本。
答案 1 :(得分:7)
在类似情况下我遇到了同样的问题。所需的HttpComponents模块已经在我的JBoss(AS 7.1.1)模块目录中。因此,我要解决的问题就是添加一个清单条目,如Eustachius所描述的那样,你可以在Maven中这样做:
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Dependencies>org.apache.httpcomponents</Dependencies>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
maven-war-plugin的配置相同。
答案 2 :(得分:2)
如果在Arquillian测试中需要一个模块......
使用以下命令在src / test / resources中创建arquillian-manifest.mf:
Manifest-Version: 1.0
Dependencies: org.jboss.resteasy.resteasy-jaxrs,org.apache.httpcomponents
然后当你ShrinkWrap:
WebArchive war = ShrinkWrap.create...
.addAsManifestResource("arquillian-manifest.mf", "MANIFEST.MF")
答案 3 :(得分:0)
但是将依赖项:org.apache.httpcomponents添加到组件的MANIFEST.MF中。
导致异常 - 引起:java.lang.IllegalStateException:JBAS014744:找不到org.apache.httpcomponents的META-INF / services / org.jboss.as.controller.Extension:main 在org.jboss.as.controller.parsing.ExtensionXml.loadModule(ExtensionXml.java:191)[jboss-as-controller-7.2.0.Final-redhat-8.jar:7.2.0.Final-redhat-8]