corner imagecorner image
FeaturesPluginsDocs & SupportCommunityPartners

Creating and Consuming RESTful Web Service jMaki Components

You can reuse RESTful client stubs in multiple web applications by wrapping the stub in jMaki components, also called a jMaki "widget." This tutorial shows you how to create a RESTful client as a jMaki widget and use the widget in other web applications.

Contents

Content on this page applies to NetBeans IDE 6.0 and 6.1

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE Web & Java EE version 6.1 or 6.0
Java Development Kit (JDK) version 6 or
version 5
GlassFish application server v2 Version v2

GlassFish can be installed with the Web and Java EE distribution of NetBeans IDE. Alternatively, you can visit the GlassFish downloads page.

This tutorial also requires the RESTful Web Services and jMaki Ajax Support plugins. In the IDE, go to the Plugin Manager, under the Tools menu, and check whether the RESTful Web Services and jMaki Ajax Support Plugins are installed, under the Installed tab. If either plugin is not installed, install it from the Available Plugins tab, as shown below for RESTful Web Services:

Adding the RESTful Web Services plugin in the plugin manager

Deploying the CustomerDB RESTful Web Service

NetBeans IDE comes with a sample CustomerDB RESTful Web Service. (Alternatively, you can create this service yourself, as shown in Getting Started with RESTful Web Services.) You can use this sample in the New Projects wizard.

To use the sample CustomerDB Service:

  1. Choose File > New Project. Under Categories, select Samples and then RESTful Web Services. Under Projects, select Customer Database, as shown.
    Selecting the Customer Database sample project in the New Project Wizard
  2. Click Next and then click Finish. The sample project opens in the IDE, with its project structure in the Projects window.
  3. You can deploy and test the web service in a single step. In the Projects window, right click the project and choose Test RESTful Web Services.
    Projects window showing Test RESTful Web Services context action

    The server starts and the IDE deploys the web service to the server. The browser then opens, displaying the test client.

  4. To test the web service, select the /customers link and then click Test. Next, select a customer link (such as /customers/1/) and click Test again. The client displays code 200, "Success."
    Browser window showing RESTful web service test client

Creating a jMaki Web Application Project

Create a jMaki framework-enabled web application to which you will add the RESTful client stubs.

To create a jMaki framework-enabled web application.

  1. Choose File > New Project. Under Categories, select Web. Under Projects, select Web Application and click Next. The Name and Application page opens
  2. In the Project Name field, type CustomerTestApp. In the Server field, select GlassFish. Leave all other values at default. Click Next. The Frameworks page opens.
  3. Select jMaki Ajax Framework and click Finish.

Generating jMaki-enabled RESTful Client Stubs for the CustomerDB Service

Create RESTful client stubs in the jMaki-enabled web application you created in the previous step.

To create the client stubs.

  1. In the Projects window, right-click the CustomerTestApp project node and choose New > Web Services > RESTful Web Services Client Stubs in the context menu.
    Selecting the New RESTful Web Service Client Stubs context action
  2. The New RESTful Web Service Client Stubs dialog opens. Click Add Project and browse for the CustomerDB Project. Leave all other settings at default and click Finish.
    The New RESTful Web Services Client Stubs dialog showing the CustomerDB project chosen and all other settings at default
  3. The IDE creates jMaki components under /CustomerTestApp/web/resources/dojo/rest. The IDE also creats an archive file named CustomerDB.zip (as shown below). The contents and the structure of the archive file are the same as the contents and the structure of the /CustomerTestApp/web directory.
    Structure of the CustomerDB.zip Archive

Testing the jMaki Client Stubs

Using NetBeans IDE, you can build, deploy and test the client stubs with a single action.

To test the client stubs.

  1. In the Projects window, navigate to TestResourcesTable.jsp. Right-click the file node and select Run File.
    Selecting the Run File context action for TestResourcesTable.jsp
  2. The server starts and the browser opens, displaying the Customers in a table, displaying the Customer table jMaki Widget. In the drop-down field, select Customers, and a table of the customers from CustomerDB opens. You can perform all CRUD (Create, Retrieve, Update and Delete) functions in this table and the RESTful Web Service propagates your changes to the database.
    Result of running TestResourcesTable.jsp, showing a table of all customers
  3. The jMaki component customerstable uses the CustomersStore dojo component (also generated by the IDE). To test the dojo component, navigate to TestResourcesTable.html, then right-click the file node and select Run File.
    Selecting the Run File context action on TestResourcesTable.html to test the dojo components

Adding the Client Stub jMaki Components to the NetBeans IDE Palette

NetBeans IDE makes it easy to reuse RESTful web service client stubs in other web applications. When you generate jMaki-enabled RESTful web service client stubs, you automatically create a jMaki component archive of the client stubs. Add the archive file to the IDE's palette for JSP and HTML pages. Then you can drag and drop the client stubs into other web applications.

To add client stub jMaki components to the IDE's palette.

  1. From the main menu, choose Tools > Palette > Add jMaki Library.
    Selecting the Add jMaki Library menu item under Tools > Palette
  2. A dialog opens in which you browse for the jMaki component archive to add to the palette. Browse for the CustomerDB.zip file, which is in the top level of the CustomerTestApp project.
    Browsing for the CustomerDB Zip file to add to the palette
  3. When you open a JSP page or HTML file, the Palette now includes the CustomerDB category, which contains the Customers and DiscountCodes services. (You may need to open the palette window from the Window menu.)
    Palette for JSP and HTML files showing CustomerDB category

Testing the jMaki Client Stubs in Another Web Application

With the CustomerDB category in the palette, you can now test how the client stub jMaki components work in another web application.

To test the jMaki Client Stubs.

  1. Choose File > New Project. Under Categories, select Web. Under Projects, select Web Application and click Next.
  2. Under Project Name, enter MyApp.
  3. Under Server, select GlassFish. Click through the remaining options and click Finish. The project's index.jsp page opens in the IDE.
  4. In the main menu, select Window > Palette. The palette opens.
  5. Drag and drop the Customers Service into the body of index.jsp.
    Dragging and dropping Customers Service into index.jsp
  6. A tag entry is now added to index.jsp. Click Ctrl-S to save the page.
    index.jsp code showing added widget
  7. In the Projects window, right-click the index.jsp node and select Run File from the context menu. The IDE builds the file and starts the server. A browser window opens, displaying the customers in a table.
    index.jsp running in a browser window, showing a table of customers


See Also

For more information about using NetBeans IDE to develop Java EE applications, see the following resources:

To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, join the mailing list.

Companion
Projects:
MySQL Database Server   GlassFish Community: an Open Source Application Server   Open Solaris  Open JDK: an Open SourceJDK   Mobile & Embedded Community     Sponsored by 
Sponsored by Sun Microsystems