Wednesday, June 6, 2012

RESTEasy Tutorial Part-1: Basics


RESTEasy Tutorial Series

RESTEasy Tutorial Part-1: Basics

RESTEasy Tutorial Part-2: Spring Integration

RESTEasy Tutorial Part 3 - Exception Handling


RESTEasy is a JAX-RS implementation from JBoss/RedHat and is in-built in JBoss 6 onwards.
Here I am going to show you how to develop a Simple RESTful Web Services application using RESTEasy and JBossAS7.1.1.FINAL.

Step#1: Configure RESTEasy dependencies using Maven.

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sivalabs</groupId>
<artifactId>resteasy-demo</artifactId>
<version>0.1</version>
<packaging>war</packaging>
<name>resteasy-demo Maven Webapp</name>
<build>
<finalName>resteasy-demo</finalName>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.2.FINAL</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>2.3.2.FINAL</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>2.3.0.GA</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>



Step#2: Configure RESTEasy in web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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">
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
</web-app>


Step#3: Create User domain class, MockUserTable class to store User objects in-memory for testing purpose and UserResource class to expose CRUD operations on User as RESTful webservices.

package com.sivalabs.resteasydemo;
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class User
{
private Integer id;
private String name;
private String email;
private Date dob;
//setters and getters
}
package com.sivalabs.resteasydemo;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sivalabs.resteasydemo.User;
public class MockUserTable
{
private static Map<Integer, User> USER_MAP = new HashMap<Integer, User>();
static
{
USER_MAP.put(1, new User(1,"admin","admin@gmail.com",new Date()));
USER_MAP.put(2, new User(2,"test","test@gmail.com",new Date()));
}
public static void save(User user){
USER_MAP.put(user.getId(), user);
}
public static User getById(Integer id){
return USER_MAP.get(id);
}
public static List<User> getAll(){
List<User> users = new ArrayList<User>(USER_MAP.values());
return users;
}
public static void delete(Integer id){
USER_MAP.remove(id);
}
}
package com.sivalabs.resteasydemo;
import java.util.List;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.sivalabs.resteasydemo.MockUserTable;
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public class UserResource
{
@Path("/")
@GET
public Response getUsersXML()
{
List<User> users = MockUserTable.getAll();
GenericEntity<List<User>> ge = new GenericEntity<List<User>>(users){};
return Response.ok(ge).build();
}
@Path("/{id}")
@GET
public Response getUserXMLById(@PathParam("id") Integer id) {
return Response.ok(MockUserTable.getById(id)).build();
}
@Path("/")
@POST
public Response saveUser(User user) {
MockUserTable.save(user);
return Response.ok("<status>success</status>").build();
}
@Path("/{id}")
@DELETE
public Response deleteUser(@PathParam("id") Integer id) {
MockUserTable.delete(id);
return Response.ok("<status>success</status>").build();
}
}



Step#6: JUnit TestCase to test the REST Webservice.

package com.sivalabs.resteasydemo;
import java.util.List;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.util.GenericType;
import org.junit.Assert;
import org.junit.Test;
import com.sivalabs.resteasydemo.User;
public class UserResourceTest {
static final String ROOT_URL = "http://localhost:8080/resteasy-demo/rest/";
@Test
public void testGetUsers() throws Exception
{
ClientRequest request = new ClientRequest(ROOT_URL+"users/");
ClientResponse<List<User>> response = request.get(new GenericType<List<User>>(){});
List<User> users = response.getEntity();
Assert.assertNotNull(users);
}
@Test
public void testGetUserById() throws Exception
{
ClientRequest request = new ClientRequest(ROOT_URL+"users/1");
ClientResponse<User> response = request.get(User.class);
User user = response.getEntity();
Assert.assertNotNull(user);
}
@Test
public void testSaveUser() throws Exception
{
User user = new User();
user.setId(3);
user.setName("User3");
user.setEmail("user3@gmail.com");
ClientRequest request = new ClientRequest(ROOT_URL+"users/");
request.body("application/xml", user);
ClientResponse<String> response = request.post(String.class);
String statusXML = response.getEntity();
Assert.assertNotNull(statusXML);
}
@Test
public void testDeleteUser() throws Exception
{
ClientRequest request = new ClientRequest(ROOT_URL+"users/2");
ClientResponse<String> response = request.delete(String.class);
String statusXML = response.getEntity();
Assert.assertNotNull(statusXML);
}
}



Step#7: To test the REST service we can use the REST Client Tool. 
You can download REST Client Tool at http://code.google.com/a/eclipselabs.org/p/restclient-tool/


Important Things to Keep in mind:
1. org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap Listener should be registered before any other listener.

2. You should configure resteasy.servlet.mapping.prefix <context-param> if the HttpServletDispatcher servlet url-pattern is anything other than /*


3. Keep visiting my blog :-)





6 comments:

  1. Hi Siva,

    thank you for your nice tutorial.
    Could you say something about deploment issues.
    I try to deploy the war file on JBoss 7.1.1 but a ClassNotFoundException is raised for the classes:

    org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher

    and

    org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap

    which are defined in the web.xml file.

    Greetings from Berlin

    Bernd

    ReplyDelete
    Replies
    1. Hi Bernd,
      Those classes should be part of JBoss-7.1.1 Server.

      org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher class can be found in jboss-as-7.1.1.Final\modules\org\jboss\resteasy\resteasy-jaxrs\main\async-http-servlet-3.0-2.3.2.Final.jar.

      org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap class can be found in jboss-as-7.1.1.Final\modules\org\jboss\resteasy\resteasy-jaxrs\main\resteasy-jaxrs-2.3.2.Final.jar.

      Delete
  2. Hi Siva,
    Thank you 4 this example, could you say something about the conversion of JSON to java & JAVA to JSON . It will be very nice if explain it via example.

    ReplyDelete
  3. Hi, Siva, thank you for this example. What do you think about adding an interface to the UserResource service and moving JAX-RS annotations to the interface? This way you can use this interface in clients to create a service proxy.

    ReplyDelete
  4. When i update from rest-easy 2.3.5.Final to rest-easy 3.0.x, the ClientRequestFactory class appear like @Deprecated.

    @Test
    public void testGetStudent() throws Exception{
    String str = "http://localhost:8080/RESTfulExample/rest/restwebservice/list";
    ClientRequest request = new ClientRequest(str);
    ClientResponse> response = request.get(new GenericType>(){});
    List students = response.getEntity();
    System.out.println("Size : "+students.size());
    }
    All the below imports are deprecated.
    import org.jboss.resteasy.util.GenericType;
    import org.jboss.resteasy.client.ClientRequest;
    import org.jboss.resteasy.client.ClientResponse;

    ReplyDelete