Java JBoss7:与ReastEasy和与自定义HttpRequestInterceptor的httpclient的加载程序约束冲突

Java JBoss7:与ReastEasy和与自定义HttpRequestInterceptor的httpclient的加载程序约束冲突,java,classloader,jboss7.x,resteasy,Java,Classloader,Jboss7.x,Resteasy,我正在JBoss-7.1.1-Final的名为@ViewScopedBean的@中使用RestEasy客户端框架,通过自定义HttpRequestInterceptor从REST服务检索数据: RegisterBuiltin.register(ResteasyProviderFactory.getInstance()); DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.addRequestIntercepto

我正在JBoss-7.1.1-Final的名为@ViewScopedBean的
@中使用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);
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());

  }
}
看起来很简单(尽管我对resteasy的打包很好奇),我添加了
org.apache.httpcomponents:httpclient
,编译范围为:

不,我得到了以下例外:

java.lang.NoClassDefFoundError: org/apache/http/HttpRequestInterceptor
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)

为了避免在JBoss上部署应用程序时出现链接错误,请在JBoss安装的modules文件夹中配置module
org.apache.httpcomponents
,但避免在应用程序中包含来自httpcomponents的JAR:

  • 将所需的来自HttpComponents的JAR放在
    modules/org/apache/HttpComponents/main
  • 在此目录中的
    module.xml
    中列出这些JAR
  • 依赖项:org.apache.httpcomponents
    添加到组件的
    MANIFEST.MF
  • 请注意,步骤1和2中提到的模块已经存在。但是,您可能希望包括其他jar(例如
    httpclient-cache-x.y.z.jar
    )或不同版本


    当然,在开发环境中解析类是另一回事。

    在类似的情况下,我也遇到了同样的问题。所需的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 jar插件
    org.apache.httpcomponents
    

    maven war插件的配置相同。

    如果在Arquillian测试中需要模块

    使用以下内容在src/test/resources中创建arquillian-manifest.mf:

    Manifest-Version: 1.0
    Dependencies: org.jboss.resteasy.resteasy-jaxrs,org.apache.httpcomponents
    
    然后,在进行包覆面处理时:

    WebArchive war = ShrinkWrap.create...
        .addAsManifestResource("arquillian-manifest.mf", "MANIFEST.MF")
    

    但是将依赖项org.apache.httpcomponents添加到组件的MANIFEST.MF中

    引起异常- 原因:java.lang.IllegalStateException:JBAS014744:未找到org.apache.httpcomponents:main的META-INF/services/org.jboss.as.controller.Extension
    在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]

    这已经是一个bug了一段时间: