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.
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:
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:
Choose File > New Project. Under Categories, select Samples and then RESTful Web Services. Under Projects, select Customer Database, as shown.
Click Next and then click Finish. The sample project opens in the IDE, with its project structure in the Projects window.
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.
The server starts and the IDE deploys the web service to the server. The browser then opens, displaying the test client.
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."
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.
Choose File > New Project. Under Categories, select Web.
Under Projects, select Web Application and click Next. The Name and Application page opens
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.
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.
In the Projects window, right-click the CustomerTestApp project node and choose New > Web Services > RESTful Web Services Client Stubs in the context menu.
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 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.
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.
In the Projects window, navigate to TestResourcesTable.jsp. Right-click the file node and select Run File.
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.
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.
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.
From the main menu, choose Tools > Palette > Add jMaki Library.
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.
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.)
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.
Choose File > New Project. Under Categories, select Web.
Under Projects, select Web Application and click Next.
Under Project Name, enter MyApp.
Under Server, select GlassFish. Click through the remaining options and click Finish. The project's index.jsp page opens in the IDE.
In the main menu, select Window > Palette. The palette opens.
Drag and drop the Customers Service into the body of index.jsp.
A tag entry is now added to index.jsp. Click Ctrl-S to save the page.
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.
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.