Lab 12: Simple RESTful Service

In this lab, you will create a simple RESTful service using JAX-RS.

Objectives:

Part 1: Creating the Service

Steps:

  1. Create a new Dynamic Web project named Lab12Web.

  2. Create a new class named HelloREST in the services package.

  3. Complete the class so it can act as a source of RESTful content:

    1. Write an annotation on the class that assigns a RESTful URL:

      @Path("/hellorest")             
                   

    2. Write a simple method that returns content:

      public String hello(String name)
      {
          return "Hello, " + name;
      }             
                   

    3. Annotate the method so that it responds to an HTTP GET and produces a text HTML result:

      @GET
      @Produces("text/html")             
                   

    4. Annotate the name parameter so it's accessible to the service as an HTTP GET parameter:

      @QueryParam("name")             
                   

    5. Import types from the javax.ws.rs package.

  4. Create another class in the services package named HelloApplication that extends javax.ws.rs.core.Application.

    Complete the class:

    1. Annotate the class to provide a prefix for the RESTful URL:

      @ApplicationPath("/rest/")             
                   

    2. Use the Source - Override/Implement Methods menu to override the getClasses method.

      You can delete the generated call to the superclass.

    3. Add code to the method so that it creates a Set containing all of your RESTful sources (only one in this case):

      Set<Class<?>> s = new HashSet<Class<?>>();
      s.add(HelloREST.class);
      return s;
                   
                   

  5. Deploy the application by adding it to the TomEE server.

  6. Test the RESTful service by opening a Web browser and entering this URL:

    http://localhost:8080/Lab12Web/rest/hellorest?name=xxxx         
             

    Substitute your name for the xxxx. You should see a simple Web page saying "Hello" to your name.