Wednesday, March 12, 2014
Monday, March 10, 2014
Getting started with RESTful webservice using Jersey
Jersey helps developing RESTful webservices seamlessly and makes your life easy. This article gives you a quick start on how to create your first RESTful web service using Jersey & Maven. I assume you have a decent understanding of maven and J2EE.
Make sure you have
Make sure you have
- Eclipse Kepler
- M2Eclipse plugin
- Java 1.7
- Tomcat 7
Step 1 - Maven Project
Create a new maven project from your eclipse. Select maven-archetype-webapp archetype, provide group id, artifact id, package in the following dialog. (See screenshot below)
Note: By default installed facets will not be Java 1.7, Dynamic web module 2.4. You can edit this in org.eclipse.wst.common.project.facet.core.xml under .settings folder. Also make sure to change the JRE from 1.5 to 1.7 in your project build path.
Step 2 - Maven Dependencies
Add the following jersey server and jersey servlet dependencies to your pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>com.sun.jersey</groupId> | |
<artifactId>jersey-server</artifactId> | |
<version>1.18</version> | |
</dependency> | |
<dependency> | |
<groupId>com.sun.jersey</groupId> | |
<artifactId>jersey-servlet</artifactId> | |
<version>1.18</version> | |
</dependency> |
Step 3 - Deployment Descriptor
Add the Jersey servlet container to your web.xml.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<web-app id="WebApp_ID" version="2.4" | |
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee | |
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> | |
<display-name>Archetype Created Web Application</display-name> | |
<servlet> | |
<servlet-name>jersey-serlvet</servlet-name> | |
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> | |
<init-param> | |
<param-name>com.sun.jersey.config.property.packages</param-name> | |
<param-value>com.ananth.jersey.rest</param-value> | |
</init-param> | |
<load-on-startup>1</load-on-startup> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>jersey-serlvet</servlet-name> | |
<url-pattern>/jersey/*</url-pattern> | |
</servlet-mapping> | |
</web-app> |
Step 4 - REST Resource
Create your package and SampleRestResource class. Add the path "/sample" at the class level, and add 2 GET resources. One with path "/hello" and the other with "/hello/{message}".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ananth.jersey.rest; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.core.Response; | |
@Path("/sample") | |
public class SampleRestResource { | |
@GET | |
@Path("/hello") | |
public Response sayHello() { | |
String output = "Your first Jersey REST application says, Hello!"; | |
return Response.status(200).entity(output).build(); | |
} | |
@GET | |
@Path("/hello/{message}") | |
public Response sayHello(@PathParam("message") String msg) { | |
String output = "Welcome: " + msg; | |
return Response.status(200).entity(output).build(); | |
} | |
} |
Step 5 - Run your application
Right click on your project --> Run As --> Run on Server --> Select tomcat. Now your first resource can be accessed from http://localhost:8080/jersey-rest-helloworld/jersey/sample/hello and the other resource from http://localhost:8080/jersey-rest-helloworld/jersey/sample/hello/your%20message
You can download the complete source here.
Subscribe to:
Posts (Atom)