无法从JBoss应用程序中调用外部JAX-RS服务

时间:2014-08-28 14:26:39

标签: jboss maven-2 client jax-rs resteasy

我遇到了很多我认为应该很容易的问题: 从我的J2E Web应用程序(无状态会话bean),我想调用外部JAX-RS服务(mailgun)。 我尝试整合泽西岛,但有重叠导入问题(和NoSuchMethodExceptions等)。所以我没有进一步追求,而是尝试了RestEasy,因为我知道这是Jboss的标准JAX-RS库。

但是,我也遇到了问题。

请您指出调用这些服务的正确途径?我非常开放使用任何库,所以如果有一个"内置"做到这一点的方式,我很高兴放下我的依赖地狱......

我目前的问题详情:

从StatelessSessionBean调用此行后:

    Client client = ClientBuilder.newClient();
    Response res = client.target("https://api.mailgun.net/v2/samples.mailgun.org" +   "/messages").request(MediaType.APPLICATION_FORM_URLENCODED_TYPE).get();   

第一行已经产生异常:

    JBAS014134: EJB Invocation failed on component EmailService for method public void de.wohnungsheld.weimea.service.EmailService.hasso(): javax.ejb.EJBException: Unexpected Error
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:163) [jboss-as-ejb3-7.1.0.Final.jar:7.1.0.Final]
...
Caused by: java.lang.NoClassDefFoundError: javax/ws/rs/client/ClientBuilder

我在Maven依赖项中玩了很多。 当前版本引用(以及其他)

 jboss-jaxrs-api_1.1_spec

 org.jboss.resteasy, version 3.0.8.Final

我认为org.jboss.resteasy在这里与其他东西相撞......

完整的依赖项部分如下所示:

 <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <scope>provided</scope>
  </dependency>

 <dependency>
        <groupId>org.jboss.spec.javax.annotation</groupId>
        <artifactId>jboss-annotations-api_1.1_spec</artifactId>
        <scope>provided</scope>
</dependency>

<dependency>
        <groupId>org.jboss.spec.javax.ws.rs</groupId>
        <artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
        <scope>provided</scope>
</dependency>

<dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Import the EJB API, we use provided scope as the API is included in 
        JBoss AS 7 -->
    <dependency>
        <groupId>org.jboss.spec.javax.ejb</groupId>
        <artifactId>jboss-ejb-api_3.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- JSR-303 (Bean Validation) Implementation -->
    <!-- Provides portable constraints such as @Email -->
    <!-- Hibernate Validator is shipped in JBoss AS 7 -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <scope>provided</scope>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Import the JSF API, we use provided scope as the API is included in 
        JBoss AS 7 -->
    <dependency>
        <groupId>org.jboss.spec.javax.faces</groupId>
        <artifactId>jboss-jsf-api_2.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Now we declare any tools needed -->

    <!-- Annotation processor to generate the JPA 2.0 metamodel classes for 
        typesafe criteria queries -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Annotation processor that raising compilation errors whenever constraint 
        annotations are incorrectly used. -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator-annotation-processor</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Needed for running tests (you may also use TestNG) -->

    <dependency>
          <groupId>com.jayway.restassured</groupId>
          <artifactId>rest-assured</artifactId>
          <version>2.3.2</version>
          <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- Optional, but highly recommended -->
    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.arquillian.protocol</groupId>
        <artifactId>arquillian-protocol-servlet</artifactId>
        <scope>test</scope>
    </dependency>


    <dependency>
      <groupId>com.fasterxml.jackson.datatype</groupId>
      <artifactId>jackson-datatype-hibernate4</artifactId>
      <version>2.4.0</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

    <dependency>
        <groupId>org.openestate.is24</groupId>
        <artifactId>ALL</artifactId>
        <version>0.1</version>
        <type>pom</type>
    </dependency>

    <dependency>
        <groupId>org.picketlink</groupId>
        <artifactId>picketlink</artifactId>
    </dependency>

    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>3.0.0.CR1</version>
        <scope>provided</scope>
    </dependency>

     <dependency>
       <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.0.8.Final</version>
          <type>pom</type>                             
        <scope>provided</scope>
    </dependency>

依赖管理如下所示:

<dependencyManagement>
    <dependencies>
            <groupId>org.jboss.bom</groupId>
            <artifactId>jboss-javaee-6.0-with-tools</artifactId>
            <version>${version.jboss.bom}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.bom</groupId>
            <artifactId>jboss-javaee-6.0-with-hibernate</artifactId>
            <version>${version.jboss.bom}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.picketlink</groupId>
            <artifactId>picketlink-javaee-6.0</artifactId>
            <version>${version.picketlink.javaee.bom}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

我正在运行JBoss AS 7.1。

非常感谢提前!

PS:我从我的应用程序中暴露了很多JAX-RS服务,工作正常。

1 个答案:

答案 0 :(得分:1)

如果问题是“为什么我得到NoClassDefFoundError”,答案是“因为依赖org.jboss.resteasy.resteasy-jaxrs被标记为已提供,因此在运行时无法使用。”

请注意,JBoss AS 7.1不使用Reasteasy版本3.0.8(在您的情况下,可能是Resteasy 2.3.5,但我不知道您使用的$ {version.jboss.bom}的价值是多少;检查$ {version.org.jboss.resteasy})的值

考虑将依赖jboss-javaee-6.0-with-resteasy添加到你的pom,或者使用jboss-javaee-6.0-all-all,所以你不需要自己设置Resteasy的版本。

如果您想更新JBoss中使用的Resteasy版本,请参阅http://docs.jboss.org/resteasy/docs/3.0.8.Final/userguide/html/Installation_Configuration.html#upgrading-as7

如果是一个选项,你也可以使用WildFly,它支持开箱即用的Resteasy 3.0.6。

相关问题