我在带有JBoss-7.1.1-Final的@Named @ViewScoped Bean中使用RestEasy Client Framework,以使用自定义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的依赖关系层次结构解析为以下内容:
httpclienthttpcore中没有WAREAR。在Bean内,我得到以下异常:
java.lang.NoClassDefFoundError: org/apache/http/HttpRequestInterceptor

看起来很简单(尽管我想知道resteasy包装),并且我添加了带有编译范围的org.apache.httpcomponents:httpclient:

不,我得到他以下异常(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)

更新要重现此代码,您不需要REST接口(interface),在实例化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());

  }
}

最佳答案

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

  • 将HttpComponents中所需的JAR放入modules/org/apache/httpcomponents/main中。
  • 在此目录中的module.xml中列出这些JAR。
  • Dependencies: org.apache.httpcomponents添加到组件的MANIFEST.MF中。

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

    解决开发环境中的类当然是另一回事。

    10-01 05:20