Jersey 2 in GlassFish 4 - First Java EE 7 Implementation Now Integrated (TOTD #182)


The JAX-RS 2.0 specification released their Early Draft 3 recently. One of my earlier blogs explained as the features were first introduced in the very first draft of the JAX-RS 2.0 specification. Last week was another milestone when the first Java EE 7 specification implementation was added to GlassFish 4 builds.

Jakub blogged about Jersey 2 integration in GlassFish 4 builds. Most of the basic functionality is working but EJB, CDI, and Validation are still a TBD. Here is a simple Tip Of The Day (TOTD) sample to get you started with using that functionality.
  1. Create a Java EE 6-style Maven project

    mvn archetype:generate
    -DarchetypeGroupId=org.codehaus.mojo.archetypes
    -DarchetypeArtifactId=webapp-javaee6 -DgroupId=example
    -DartifactId=jersey2-helloworld -DarchetypeVersion=1.5
    -DinteractiveMode=false

    Note, this is still a Java EE 6 archetype, at least for now.
  2. Open the project in NetBeans IDE as it makes it much easier to edit/add the files. Add the following <repositories>

    <repositories>
    <repository>
    <id>snapshot-repository.java.net</id>
    <name>Java.net Snapshot Repository for Maven</name>
    <url>https://maven.java.net/content/repositories/snapshots/</url>
    <layout>default</layout>
    </repository>
    </repositories>

  3. Add the following <dependency>s

    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0-m09</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.0-m05</version>
    <scope>test</scope>
    </dependency>
    The complete list of Maven coordinates for Jersey2 are available here. An up-to-date status of Jersey 2 can always be obtained from here.
  4. Here is a simple resource class:

    @Path("movies")
    public class MoviesResource {

    @GET
    @Path("list")
    public List<Movie> getMovies() {
    List<Movie> movies = new ArrayList<Movie>();

    movies.add(new Movie("Million Dollar Baby", "Hillary Swank"));
    movies.add(new Movie("Toy Story", "Buzz Light Year"));
    movies.add(new Movie("Hunger Games", "Jennifer Lawrence"));

    return movies;
    }
    }
    This resource publishes a list of movies and is accessible at "movies/list" path with HTTP GET. The project is using the standard JAX-RS APIs.

    Of course, you need the trivial "Movie" and the "Application" class as well. They are available in the downloadable project anyway.
  5. Build the project

    mvn package

    And deploy to GlassFish 4.0 promoted build 43 (download, unzip, and start as "bin/asadmin start-domain") as

    asadmin deploy --force=true target/jersey2-helloworld.war

  6. Add a simple test case by right-clicking on the MoviesResource class, select "Tools", "Create Tests", and take defaults. Replace the function "testGetMovies" to
    @Test
    public void testGetMovies() {
    System.out.println("getMovies");
    Client client = ClientFactory.newClient();
    List<Movie> movieList = client.target("http://localhost:8080/jersey2-helloworld/webresources/movies/list")
    .request()
    .get(new GenericType<List<Movie>>() {});
    assertEquals(3, movieList.size());
    }

    This test uses the newly defined JAX-RS 2 client APIs to access the RESTful resource.
  7. Run the test by giving the command "mvn test" and see the output as

    -------------------------------------------------------
    T E S T S
    -------------------------------------------------------
    Running example.MoviesResourceTest
    getMovies
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.561 sec

    Results :

    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

GlassFish 4 contains Jersey 2 as the JAX-RS implementation. If you want to use Jersey 1.1 functionality, then Martin's blog provide more details on that. All JAX-RS 1.x functionality will be supported using standard APIs anyway. This workaround is only required if Jersey 1.x functionality needs to be accessed.

The complete source code explained in this project can be downloaded from here.

Here are some pointers to follow

Provide feedback on Jersey 2 to users@jersey.java.net and JAX-RS specification to users@jax-rs-spec.java.net.

Comments:

Hi Arun, there's a typo in step 2 "respositories".

Posted by Stephen on July 06, 2012 at 12:14 AM PDT #

Thanks Stephen, fixed!

Posted by Arun Gupta on July 06, 2012 at 07:00 AM PDT #

This is what I've been looking for. Thank you so much for sharing it. Now I don't have to Google it up further as you've finally showed me the answer. I'm a newbiew at this stuff, you see.

Posted by Nadine on August 22, 2012 at 12:17 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« March 2015
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    
       
Today