4

我在@Named @ViewScoped带有 JBoss-7.1.1-Final 的 Bean 中使用 RestEasy 客户端框架,通过自定义从 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 服务)。该 bean 位于 aWAR内的一个模块中EAR,resteasy 的依赖层次结构解析为以下内容:

Resteasy依赖

httpclientorhttpcore中没有WARor EAR。在 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());

  }
}
4

4 回答 4

10

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

  1. 将来自 HttpComponents 的所需 JAR 放在modules/org/apache/httpcomponents/main.
  2. module.xml在此目录中列出这些 JAR 。
  3. 添加Dependencies: org.apache.httpcomponentsMANIFEST.MF您的组件中。

请注意,步骤 1 和 2 中提到的模块已经存在。但是,您可能希望包含其他 JARS(例如httpclient-cache-x.y.z.jar)或不同版本。

在您的开发环境中解析类是另一回事。

于 2012-05-29T14:10:44.903 回答
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 的配置相同。

于 2012-08-02T01:24:33.620 回答
2

如果在 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")
于 2012-10-01T14:42:54.853 回答
0

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

导致异常 - 原因:java.lang.IllegalStateException: JBAS014744: No META-INF/services/org.jboss.as.controller.Extension found for org.apache.httpcomponents:main at 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]

于 2014-10-31T02:09:49.077 回答