Revision as of 18:33, 6 November 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

J1 REST Demo for SWDP, NetBeans, Enterprise Pack

Hightlights of Values:

  1. Instant REST web services from persistence entity classes.
  2. Clean and flexible converter architecture that could be extended to add any type of representation.
  3. Iterative REST service development cycle with generic test client.
  4. REST resource composition with existing well-known resources, such as Google Map.


0. Required Installations:

1. Create a Web Application:

  • Click on 'New Project' button to bring up the wizard, select 'Web' category and 'Web Application' project type. Click 'Next'.
  • Type 'CustomerDB' for project name; select Sun Java System Application Server/GlassFish for target server and 'Java EE 5' for Java EE version. Click 'Finish'.


  • SWDP could be installed to any web container.

2. Create Entity Classes:

  • Right click on 'CustomerDB' project and do 'New > Entity Classes from Database...'
  • Select 'jdbc/sample' for Data Source field.
  • Select 'CUSTOMER' in Available Tables column and click 'Add'. Click 'Next'.
  • Click 'Create Persistence Unit' and accept all default values.
  • Type in non-default package name and click 'Finish'.


  • Java DB (Derby) integration is included as part of NetBeans bundle. JDBC driver for Java DB, is included by both NetBeans 6.0 and AppServer.

Demo script:

1. Introduction (attached slide is optional, you can just talk):

  • REST (Representational State Transfer) is a simple architectural style for Web Services.
  • In a REST-ful application, resources are identified by a URI, accessed through fixed set of HTTP methods: (GET, POST, PUT, DELETE), and realized as representations.
  • REST Java API specification (JSR-311) and its reference implementation are provided through Sun Web Developer Pack (SWDP): http://developers.sun.com/web/swdp


  • REST is a resource-oriented architecture for implementing web services based on a fixed set of HTTP interfaces (GET, POST, PUT, DELETE).
  • NetBeans 6.0 support iterative REST development cycle: code generation, extending, deployment and testing.

2. Create REST web services from Entity classes

  • Right click on the package containing entity classes, do 'New > REST Web Services from Entity Classes'. Click 'Select All' and then 'Next'.
  • Accept all default and click 'Finish'. Two new packages are generate 'services' and 'converter'.


  • "Under the cover, the wizard instropect the entity relationship information to help generate resource classes and representation converter. The generated code are organized into clean architectural layers: service and converter."
  • Expand the services package and show CustomersResource and CustomerResource (as container-item pair).
  • Note annotation @UriTemplate with URI value or pattern to identify an instance of the resource.
  • Note the use of @HttpMethod to mark resource public access methods.
  • Container resource has only GET and POST methods. Item resource has 3 methods: GET, PUT, DELETE. These resource interfaces allow CRUD (create/retrieve/update/delete) accesses to the database.
  • Representation type could be specified using @ProduceMime and @ConsumeMime.
  • Expand the converter package and show typical entity, container and reference converter classes: CustomerConverter, CustomersConverter, CustomerRefConverter.
  • "Converter layer help marshaling/unmarshaling between entity data and XML representation. More importantly it re-normalize JAXB in-line entity reference into URI value: the ID of the REST world".

3. Test REST Web Services:

  • Right click on the project and do 'Test REST Web Services'. The web application will be built, deployed and the default browser will be brought up with the javascript test client.


  • Under cover, the IDE processes the JSR-311 annotations and configure the web app. Then it proceeds to build the web archive, deploys it to GlassFish web container, generates the test client and launch the browser.
  • Each node on the left frame is a root container resource.
  • Maximize the test client browser window. Click on 'customers' folder on the left frame. On the right frame, click on 'Test' button.
  • In the 'Test Output', show each of the tabs: 'Tabular View', 'Raw View', 'Headers'.
  • Click on '/customers/1' link in 'Tabular View' and click on 'Test' to retrieve the resource for Customer with CustomerId 1.
  • Click on 'Raw View' tab to show the resource '/customers/1' XML representation.
  • Show pull-down menus for available HTTP methods and Mime types.
  • "In summary, the test client provide URI navigation between related resources and invocation of resource interface methods. You can click any relative URI either in output area, the left frame or the history, to set current resource for testing."

4. Invoking Google Map Resource (optional):

  • Open ManufacturerResource.java
  • Right click anywhere in the editor and do 'REST Web Services' > 'Add Google Map'.
  • Enter 'ABQIAAAAwP8gVYesAIuBNOlJaZvj8RSk_Deds9QsEdskwuMIQ4x8tiSx9RR7LXTlsVufo1jcJP50ZdJCuS7dEA' for Google map key and proxy if any.
  • Edit the method 'getMap' from:
  public GoogleMapResource getMap() {
        String address = "16 Network Cir, Menlo Park, CA";
	// . . .


  public GoogleMapResource getMap(@UriParam("manufacturerId") Integer id) {
        Manufacturer m = getEntity(id);
        String address = m.getAddressline1()+", "+m.getCity()+", "+m.getState();
	// . . .
  • Do project context menu 'Deploy', then navigate to one of manufacturer resources and invoke method 'GET'.
  • In Test Output area, the tabular view show a googleMap URI, click on it and invoke GET. The raw view now show the Google map of the manufacturer address.


  • The GoogleMapResource has the GET method that return HTML representation with JavaScript to invoke the GoogleMap API.
  • The ManufactureResource exposes the GoogleMapResource for its address attribute as a sub-resource with URI "manufacturers/{manufacturerId}/googleMap".

Demo Video


Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo