RESTful Web Services Developer's Guide

The Bookstore Application

The Bookstore web application shows how to connect JSP pages to resources. The Bookstore web application presents books, CDs, and tracks from CDs. The sample application consists of five web resources, described and outlined in the following section.

This sample demonstrates how to support polymorphism of resources and JSP pages, which means that it allows values of different data types to be handled using a uniform interface. The use of polymorphism makes it possible to add another resource, such as a DVD resource with associated JSP pages, which would extend Item without having to change the logic of Bookstore or any of the existing JSP pages.

Web Resources for Bookstore Application

The Bookstore resource returns a list of items, either CDs or books. The resource dynamically references a Book or CD resource using the getItem method that is annotated with @Path. Both the Book and CD resources inherit from the Item class, therefore, the item can be managed polymorphically.

The following snippet of code from com.sun.jersey.samples.bookstore.resources.Bookstore.java adds some items to the bookstore application and, using a URI path template to pass in the item ID, provides a method for retrieving the item.

@Path("/")
@Singleton
public class Bookstore {    
    private final Map<String, Item> items = new TreeMap<String, Item>();
    
    private String name;
    
    public Bookstore() {
        setName("Czech Bookstore");
        getItems().put("1", new Book("Svejk", "Jaroslav Hasek"));
        getItems().put("2", new Book("Krakatit", "Karel Capek"));
        getItems().put("3", new CD("Ma Vlast 1", "Bedrich Smetana", new Track[]{
            new Track("Vysehrad",180),
            new Track("Vltava",172),
            new Track("Sarka",32)}));
    }
    
    @Path("items/{itemid}/")
    public Item getItem(@PathParam("itemid") String itemid) {
        Item i = getItems().get(itemid);
        if (i == null)
            throw new NotFoundException("Item, " + itemid + ", is not found");
        
        return i;
    }
   . . . 
  }

Both the Book and the CD resources inherit from the Item class. This allows the resources to be managed polymorphically. The Book resource has a title and an author. The CD resource has a title, author, and list of tracks. The Track resource has a name and a track length.

The following code snippet from the CD resource dynamically references the Track resource using the getTrack method that is annotated with @Path.

public class CD extends Item {
    
    private final Track[] tracks;
    
    public CD(final String title, final String author, final Track[] tracks) {
        super(title, author);
        this.tracks = tracks;
    }
    
    public Track[] getTracks() {
        return tracks;
    }
    
    @Path("tracks/{num}/")
    public Track getTrack(@PathParam("num") int num) {
        if (num >= tracks.length)
            throw new NotFoundException("Track, " + num + ", 
            of CD, " + getTitle() + ", is not found");
        return tracks[num];
    }    
}

Mapping the URI Path in the Bookstore Example

JSP pages are associated with resource classes. These JSP pages are resolved by converting the fully-qualified class name of the resource class into a path and appending the last path segment of the request URI path to that path. For example, when a GET is performed on the URI path "/", the path to the JSP page is /com/sun/jersey/samples/bookstore/resources/Bookstore/. For this example, since the last path segment is empty, index.jsp is appended to the path. The request then gets forwarded to the JSP page at that path. Similarly, when a GET is performed on the URI path count, the path to the JSP page is /com/sun/jersey/samples/bookstore/resources/Bookstore/count.jsp.

The JSP variable it is automatically set to the instance of Bookstore so that the index.jsp, or count.jsp, has access to the Bookstore instance as a Java bean.

If a resource class inherits from another resource class, it will automatically inherit the JSP pages from the super class.

A JSP page may also include JSP pages using the inheritance mechanism. For example, the index.jsp page associated with the Book resource class includes a footer.jsp page whose location is specified by the super class, Item.

The mapping of the URI path space is shown in the following table.

URI Path 

Resource Class 

HTTP method 

Bookstore 

GET 

/count 

Bookstore 

GET 

/time 

Bookstore 

GET 

/items/{itemid} 

Book, CD 

GET 

/items/{itemid}/tracks/{num} 

Track 

GET 

Mapping the URI Paths and JSP Pages

The mapping of the URI paths and JSP pages is shown in the following table.

URI Path  

JSP Page 

/com/sun/jersey/samples/bookstore/resources/Bookstore/index.jsp 

/count 

/com/sun/jersey/samples/bookstore/resources/Bookstore/count.jsp 

/time 

/com/sun/jersey/samples/bookstore/resources/Bookstore/time.jsp 

/items/{itemid} 

/com/sun/jersey/samples/bookstore/resources/Book/index.jsp 

/items/{itemid} 

/com/sun/jersey/samples/bookstore/resources/CD/index.jsp 

/items/{itemid}/tracks/{num} 

/com/sun/jersey/samples/bookstore/resources/Track/index.jsp 

ProcedureBuilding and Running the Bookstore Application from a Terminal Window

  1. Open a terminal prompt and navigate to as-install/jersey/samples/bookstore.

  2. Enter mvn glassfish:run and press Enter.

    This will build, package, and deploy the project onto GlassFish using Maven.

  3. In a web browser navigate to:


    http://localhost:8080/Bookstore/

ProcedureBuilding and Running the Bookstore Application from NetBeans IDE

  1. Select File->Open Project, and browse to the Jersey Bookstore sample application directory to open the project.

  2. Right-click the project and select Properties. On the Actions tab, make sure that Use external Maven for build execution is checked. Also on the Actions tab, select Run Project under Actions, then change the Execute Goals field to package glassfish:run, and change the Set Properties field to netbeans.deploy=true. On the Run tab, make sure that the server is set to GlassFish v3 Prelude.

  3. Right-click the project and select Run.

    This will build, package, and deploy the project onto GlassFish using Maven.

  4. In a web browser navigate to:


    http://localhost:8080/Bookstore/

    When the sample is running, it looks like the following example:


    URL: http://locahlhost:8080/Bookstore/
    Czech Bookstore
    
    Item List
    
    	* Svejk
    	* Krakatit
    	* Ma Vlast 1
    
    Others
    
    count inventory
    get the system time
    regular resources