Jersey RESTful Web Services framework is open source, production quality, framework for developing RESTful Web Services in Java.
The Jersey project is part of Glassfish project sponsored by Oracle Corporation.
Jersey framework provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation.
For an introduction of REST architecture please refer to REST Architecture
Developing REST using Jersey Framework and Eclipse (Quick Start for Non Maven developers)
Google search for “Download jersey client” and find the Jersey download site or download it from Jersey Project Site. The current version while writing this article is Jersey 2.22.2
Click on “Jersey JAX-RS 2.0 RI bundle” download link. Save the “jaxrs-ri-2.22.2.zip” and extract it. Inside jaxrs-ri directory, there will be three directories api, ext and lib. From Jersey version 2.7 all the Jersey components are compiled with Java SE 7. So for this tutorial we are using Java SE 7.
I am using eclipse-jee-mars-1-win32-x86_64 for this tutorial.
1. Open eclipse and create a dynamic web project called myRestApp.
File -> New -> Dynamic Web Project. Provide the project name as “myRestApp”. Click Next twice. In the Web Module panel select the checkbox to “Generate web.xml deployment descriptor”. Then click Finish.
2. Under the project’s WEB-INF/lib directory copy all the jar files from all the three directories of the unzipped Jersey binaries.
a. /jaxrs-ri-2.22.1/jaxrs-ri/api
b. /jaxrs-ri-2.22.1/jaxrs-ri/ext
c. /jaxrs-ri-2.22.1/jaxrs-ri/lib
3. Under the src folder create a package of your choice. I am creating it as com.jsession4d. The eclipse screenshot is provided below.
4. Right click on the package and create a new Class file called “Hello”. The code is provided below.
package com.jsession4d; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; @Path("Hello") public class Hello { @GET @Path("/{Name}") @Produces("text/plain") public String sayHello(@PathParam("Name") String name) { return "Hello " + name; } }
5. Edit the web.xml file under WEB-INF as shown below.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>myRestApp</display-name> <servlet> <servlet-name>Jersey myRestApp</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.jsession4d</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Jersey myRestApp</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
6. Now right click the project “myRestApp” and select “Run As” and then “Run On Server”.
I have already configured Tomcat in eclipse using the steps provided here. Configure Tomcat in Eclipse workspace
(OR)
Right Click the project “myRestApp”, Click Export, then Click War file. Save the “myRestApp.war” War file under “webapps” directory of Tomcat. Then start Tomcat.
Now your REST application is ready to be accessed.
7. Open a browser and enter the below URL. If you get the response “Hello Mike”, you are all set and ready with your hello world REST application using Jersey.
http://localhost:8080/myRestApp/Hello/Mike