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.
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.
Open the project in NetBeans IDE as it makes it much easier to
edit/add the files. Add the following <respositories>
<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>
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.
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.
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
Add a simple test case by right-clicking on the MoviesResource
class, select "Tools", "Create Tests", and take defaults.
Replace the function "testGetMovies" to
@Testpublic 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.
Run the test by giving the command "mvn test" and see the
output as
------------------------------------------------------- T E S T S-------------------------------------------------------Running example.MoviesResourceTestgetMoviesTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.561 secResults :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
JAX-RS
2 Specification Early Draft
3
Latest status on
specification (jax-rs-spec.java.net)
Latest
JAX-RS 2.0 Javadocs
Latest status on Jersey
(Reference Implementation of JAX-RS 2 - jersey.java.net)
Latest
Jersey API Javadocs
Latest
GlassFish 4.0 Promoted Build
Follow @gf_jersey
Provide feedback on Jersey 2 to
[email protected]
and JAX-RS specification to
[email protected].