Build RESTful web service with jersey 2

You need following tools and techniques for this tutorial

  • Eclipse Kepler( that already have maven plugin m2e if you using older version should install that plugin)
  • Maven
  • Jersey 2
  • Tomcat 7

Let’s start with Part 1: Build server application 1.Create maven web application In eclipse IDE choose create maven project maven-webapp Choose web application skeleton Enter follow info jersey-maven 2.Dependencies Add follow dependencies to our pom.xml file

        <dependencies>
		<dependency>
			<groupId>org.glassfish.jersey.containers</groupId>
			<artifactId>jersey-container-servlet</artifactId>
			<version>2.7</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provide</scope>
		</dependency>
		<dependency>
			<groupId>org.glassfish.jersey.media</groupId>
			<artifactId>jersey-media-json-jackson</artifactId>
			<version>2.7</version>
		</dependency>
	</dependencies>

3.Create Jersey Resources We create model User

public class User implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5844780011894209010L;
	private Long id;
	private String name;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

To process RESTful action GET,PUT,POST,DELTE we create following class with appropriate annotation

@Path("/user")
public class UsersResource {
	/*
	 * Retrieve User
	 */
	@GET
	@Path("/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public User get(@PathParam(value = "id") Long id) {
		/*create sample to return*/
		User user = new User();
		user.setId(id);
		user.setName("ThinhPT");
		return user;
	}

	/*
	 * Create User
	 */
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public User create(User bean) {
		return bean;
	}

	/*
	 * Update User
	 */
	@PUT
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public User update(User bean) {
		return bean;
	}
}

4. Web application config We change web.xml as below

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	      version="3.0">
	<display-name>Archetype Created Web Application</display-name>
	<servlet>
		<servlet-name>Jersey Web Application</servlet-name>
		<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
		<init-param>
			<param-name>javax.ws.rs.Application</param-name>
			<param-value>com.devjav.jersey.MainApp</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Jersey Web Application</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
</web-app>

5. Source code You can get source code on this link

One thought on “Build RESTful web service with jersey 2”

  1. Pingback: Java Dev world

Leave a Reply