X

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

Guest Author


The JAX-RS 2.0 specification released their href="http://jcp.org/aboutJava/communityprocess/edr/jsr339/index3.html">Early
Draft 3 recently. One of my href="https://blogs.oracle.com/arungupta/entry/jax_rs_2_0_early">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 href="https://blogs.oracle.com/japod/entry/jersey_2_0_integrated_into">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 style="font-weight: bold;">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 href="http://search.maven.org/#search%7Cga%7C1%7Corg.glassfish.jersey">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 href="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/File/536181782352701534846a24f6d0a9f0/totd182_jersey2_helloworld.zip">downloadable
    project anyway.
  5. Build the project


    mvn package



    And deploy to href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/glassfish-4.0-b43.zip">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 href="http://blog.alutam.com/2012/06/26/writing-web-applications-that-can-run-with-jersey-1-x-as-well-as-jersey-2-0/">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 href="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/File/536181782352701534846a24f6d0a9f0/totd182_jersey2_helloworld.zip">downloaded
from here.

Here are some pointers to follow

  • href="http://jcp.org/aboutJava/communityprocess/edr/jsr339/index3.html">JAX-RS
    2 Specification Early Draft 3
  • Latest status on
    specification
    (jax-rs-spec.java.net)

  • href="http://jax-rs-spec.java.net/nonav/2.0-SNAPSHOT/apidocs/index.html">Latest
    JAX-RS 2.0 Javadocs
  • Latest status on Jersey
    (Reference Implementation of JAX-RS 2 - jersey.java.net)
  • href="http://jersey.java.net/nonav/apidocs/snapshot/jersey/index.html">Latest
    Jersey API Javadocs
  • href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/latest-glassfish.zip">Latest
    GlassFish 4.0 Promoted Build

  • Follow @gf_jersey

Provide feedback on Jersey 2 to href="http://java.net/projects/jersey/lists/users/archive">users@jersey.java.net
and JAX-RS specification to href="http://java.net/projects/jax-rs-spec/lists/users/archive">users@jax-rs-spec.java.net.

Join the discussion

Comments ( 3 )
  • Stephen Friday, July 6, 2012

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


  • Arun Gupta Friday, July 6, 2012

    Thanks Stephen, fixed!


  • Nadine Wednesday, August 22, 2012

    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.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.