Overview
Jersey is a Java framework for developing RESTful web services. It is an implementation of JAX-RS reference. In addition to JAX-RS , Jersey has its extended APIs that eases the development of restful web services and client development. In this tutorial, I will guide you through the steps to jump-start developing the RESTful web services using the Jersey framework.Requirements for this tutorial
- Eclipse
- Maven
- Jersey Framework
- Tomcat or any other Servlet container
STEP 1
Open the Eclipse and create an empty Maven web project. Add the following dependencies
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${jaxrs.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey2.version}</version>
</dependency>
</dependencies>
STEP 2
Create a class with the following code
package com.techieshah;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import com.techieshah.domainmodel.BlogPost;
/*Author: https://techieshah.blogspot.com*/
@Path("/")
public class JerseyExample {
@GET
public BlogPost get(@QueryParam("id") Integer id){
for(BlogPost blogPost : BlogPostRepository.blogPosts) {
if(blogPost.id == id) {
return blogPost;
}
}
return null;
}
@POST
public String post( BlogPost blogPost){
BlogPostRepository.blogPosts.add(blogPost);
return "Blog Post saved successfully";
}
@PUT
public String put(BlogPost updatedBlogPost) {
for(BlogPost blogPost : BlogPostRepository.blogPosts) {
if(blogPost.id == updatedBlogPost.id) {
blogPost.name = updatedBlogPost.name;
blogPost.tag = updatedBlogPost.tag;
blogPost.content = updatedBlogPost.content;
}
}
return "Blog Post updated successfully";
}
@DELETE
public String delete(@QueryParam("id") Integer id){
for(BlogPost blogPost : BlogPostRepository.blogPosts) {
if(blogPost.id == id) {
BlogPostRepository.blogPosts.remove( blogPost );
break;
}
}
return "Blog Post deleted successfully";
}
}
@Path("/")
This specifies the URL path for this resource.
@QueryParam
This is used to specify the Query parameters in the URL
The following annotations are used to map the class functions against the corresponding HTTP methods
@GET (for selection), @POST (for insert) , @PUT (for update) and @DELETE (for delete)
STEP 3
Modify the web.xml
<servlet>
<servlet-name>JerseyService</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.techieshah</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyService </servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
Now our web services are ready for testing. We can use any Rest client to test it.
Request
GET http://localhost:9080/jerseywsexample/api?id=1
Response
<blogPost>
<id>1</id>
<name>spring framework</name>
<tag>ws</tag>
<content>first example of rest web services</content>
</blogPost>
Similarly, we can test the remaining methods i.e. POST, PUT and DELETE.
Complete source code can be downloaded from Jersey JAX-RS Example .
Comments
Post a Comment