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.




Step#2: Configure RESTEasy in web.xml



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.




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




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