2012-05-15 78 views
4

我使用RestEasy的客戶端框架的@Named @ViewScoped豆與JBoss-7.1.1-最後用一個自定義的HttpRequestInterceptor檢索REST服務的數據:JBoss7:裝載機約束衝突與ReastEasy和HttpClient的定製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服務)。所述豆是在EAR內部的WAR模塊,RestEasy的的依賴性的層次結構解析爲以下:

Resteasy dependency

中有WAREAR沒有httpclienthttpcore。在Bean裏面我得到以下異常:

java.lang.NoClassDefFoundError: org/apache/http/HttpRequestInterceptor 

好像容易(雖然我不知道有關RestEasy的包裝),我加入org.apache.httpcomponents:httpclient編譯範圍:

enter image description here

不,我得到他以下異常:

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

This已經有一段時間了:https://issues.jboss.org/browse/AS7-2803 – Phyxx

回答

10

爲了避免鏈接錯誤時部署 JBoss上的應用中,JBoss安裝的模塊文件夾內的配置模塊org.apache.httpcomponents,但要避免包括HttpComponents JAR文件與應用程序:

  1. 廣場HttpComponents所需的JAR文件中modules/org/apache/httpcomponents/main
  2. 在這個目錄下的module.xml列出這些JAR。
  3. Dependencies: org.apache.httpcomponents添加到您的組件的MANIFEST.MF

請注意,步驟1和步驟2中提到的模塊已經存在。但是您可能想要包含其他JARS(例如httpclient-cache-x.y.z.jar)或不同的版本。

解決您的開發環境中的類是另一回事。

+0

謝謝!並歡迎在我們的社區:-) – Thor

6

在類似情況下,我遇到了同樣的問題。所需的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的戰爭插件相同。

+0

完美,謝謝@codemiller,爲我做到了! – Gregor

2

如果需要在測試的Arquillian模塊...

與下列創建的src /測試/資源的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") 
0

但添加依賴項:org.apache.httpcomponents到組件的MANIFEST.MF。

引起異常 - 產生的原因:java.lang.IllegalStateException:JBAS014744:沒有META-INF /服務/ org.jboss.as.controller.Extension發現org.apache.httpcomponents:主要 在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]

相關問題