jakarta-eejax-rsglassfish-embedded

Testing Restful client from EJB3


I do not understand why when I am creating a JAX-RS client inside an EJB, cannot get the implementation of JAX-RS. I believe that it is assumed that an application server like Glassfish 3.1 should provide an JAX-RS implementation like Jersey, and I should not add it like dependency, but it can not find it.

The error is a java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder and I think that It should not be necessary add Jersey to the classpath because Glassfish must provide it.

The code that generates the error is

Client client = ClientBuilder.newClient();

This code is inside an EJB method, and my test case is:

EJBContainer container = EJBContainer.createEJBContainer();
MyService service = (MyService) container.getContext().lookup("java:global/classes/MyService");
service.create(null);

The dependencies in the pom.xml file looks like:

<dependencies>
    <dependency>
        <groupId>org.glassfish.extras</groupId> 
        <artifactId>glassfish-embedded-all</artifactId> 
        <version>3.1.1</version> 
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>1.7.7</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.12</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

The packaging is a WAR file, I think that should not be a problem.


Solution

  • GlassFish 3.1 is a JavaEE 6 implementation which implements JAX-RS 1.1

    You need to update to GlassFish 4.1 to get the JAX-RS 2.0 implementation that you are using (according to the javaee-api 7.0 dependency that you have included).

    In fact javax.ws.rs.client.ClientBuilder was added in JAX-RS 2.0.