blob: 061b205a6bf690b24de34ecd11782b3f87aab9d2 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Title" content="Buildcing RESTful Web Services in NetBeans 6.0"/>
<meta name="Author" content="Peter Liu, peter.liu@sun.com) "/>
<meta name="description" content="Rapid code generation based on patterns and JPA entities, easier testing with Test Client, invoking third-party services with RESTful components and building client applications using generated JavaScript client libraries"/>
<meta name="copyright" content="© 2007 NetBeans"/>
<meta name="Designer" content="pH Design"/>
<link rel="stylesheet" type="text/css" href="../magazine.css" media="screen"/>
<title>NetBeans Magazine, Issue 4 . Building RESTful Web Services in NetBeans 6.0 </title>
</head>
<body>
<div id="nb04-restfulws">
<div class="image"><img src="../../../../images_www/magazine/issue04/top-restfulws.jpg" alt="" width="770" height="200" /></div>
<div class="image"></div>
<div class="image"></div>
<div class="image"></div>
<div class="story"></div>
<div class="story">
</div>
<div class="story">
<p class="nb-body"><span class="nb-body-bold">R</span>epresentational State Transfer (REST), you will recall, is an increasingly popular architectural style for distributed hypermedia systems such as the Web. Central to the RESTful architecture style is the concept of resources identified by universal resource identifiers (URIs). Resources can be manipulated using a standard interface, such as HTTP, and information is exchanged using representations of these resources. Building web services using the RESTful approach is emerging as a popular alternative to using SOAP-based technologies for deploying services on the Internet, due to its lightweight nature.</p>
<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#000033">
<tbody>
<tr>
<td bgcolor="#000033"><p class="nb-table-title">More on REST<br />
</p></td>
</tr>
<tr align="left">
<td valign="top" bordercolor="#869D00" bgcolor="#869D00"><div class="story">
<p class="nb-table-body1">RESTful web services are much simpler than the traditional SOAP-based ones. There is of course no SOAP and no WSDL, and definitely no WS-* standards – just simple message exchange over HTTP using any format you want: XML, JSON, HTML, etc. RESTful web services are really just a collection of web resources identifiable by URIs, which can be manipulated by a small number of operations – GET, PUT, POST and DELETE (corresponding to commons actions you can specify in a HTTP header). Because of this, “resource” is used throughout the article to mean a RESTful web service.</p>
</div></td>
</tr>
</tbody>
</table>
<p class="nb-body">NetBeans 6.0, with the addition of a specialized plugin, supports the rapid development of RESTful web services using JSR-311 (Java API for RESTful Web Services – JAX-RS) and Jersey, the reference implementation for JAX-RS. The IDE supports building and testing services, as well as creating client applications that access these services, and generating code for invoking web services (not only RESTful but also SOAP-based). The following are the RESTful features provided:</p>
<p class="nb-body">• Rapid creation of RESTful web services from Java Persistence API entity classes and patterns. <br />
• Fast code generation for invoking web services such as Google Maps, Yahoo News Search, and StrikeIron services, by drag-and-dropping components from the RESTful component palette.<br />
• Generation of JavaScript client stubs from RESTful web services for building client applications. <br />
• Test client generation for testing RESTful web services. <br />
• A logical view for easy navigation of RESTful web service implementation classes in your project. </p>
<p class="nb-body">In this article, we will outline the steps to use these features to create RESTful applications.</p>
<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#000033">
<tbody>
<tr>
<td bgcolor="#000033"><p class="nb-table-title">More on REST</p></td>
</tr>
<tr align="left">
<td valign="top" bgcolor="#869D00"><div class="story">
<p class="nb-table-body1">RESTful web services are much simpler than the traditional SOAP-based web services. There is of course no SOAP and no WSDL, and definitely no WS-* standards – just simple message exchange over HTTP using any format you want: XML, JSON, HTML, etc. RESTful web services are really just a collection of web resources identifiable by URIs, which can be manipulated by a small number of operations – GET, PUT, POST and DELETE (corresponding to commons actions you can specify in a HTTP header). Because of this, you will see me use resources to mean RESTful web services throughout the article.</p></div></td>
</tr>
</tbody>
</table>
<p class="nb-section">Getting the RESTful Web Services Plugin</p>
<p class="nb-body">Since we are tracking JSR-311 which is still an unfinished specification at the time of writing, the RESTful Web Services plugin is only available via the NetBeans 6.0 Plugin Manager from the Update Center. To download it, you need to have installed NetBeans 6.0 in the Web and Java EE or Complete releases. Once in the IDE, go to Tools&gt;Plugins and install the plugin, as shown in <span class="nb-body-bold">Figure&nbsp;1</span>. You’ll then be ready to explore the various features available for developing and testing your RESTful applications.</p>
<p class="nb-body"><span class="image"><img src="../../../../images_www/magazine/issue04/image18913_opt.jpeg" alt="Figure" /></span><br />
<span class="nb-body-bold">Figure 1.</span> Downloading the RESTful Web Services Plugin from the Plugin Manager</p>
<div class="story">
<p class="nb-eye">The RESTful Web Services Plugin comes bundled with the latest Jersey libraries, so you do not need to install the Jersey runtime in your application server in order to run your applications.</p>
</div>
<p class="nb-section">A first RESTful Web Service</p>
<p class="nb-body">Start by creating a Web project with the type Web Application and default settings. From the Web project node, choose <span class="nb-body-italico">New&gt;RESTful Web Services from Patterns</span>. <span class="nb-body-bold">Figure&nbsp;2</span> shows the first panel of the wizard. As you’ll see, three patterns are currently supported: Singleton, Container-Item and Client-Controlled Container-Item (see more about these in the “RESTful patterns” <span class="nb-body-bold">box</span>). We will use the Singleton pattern to create a “Hello World” service. Click on the Next button to go to the panel shown in <span class="nb-body-bold">Figure&nbsp;3</span>.</p>
<p class="nb-body"><span class="image"><img src="../../../../images_www/magazine/issue04/image18919_opt.jpeg" alt="Figure" width="328" height="265" /><br />
</span><span class="nb-body-bold">Figure 2. </span><span class="image"> Pattern Selection Panel<br />
</span></p>
<p class="nb-body"><span class="image"><img src="../../../../images_www/magazine/issue04/image18925_opt.jpeg" alt="Figure" /><br />
</span><span class="nb-body-bold">Figure 3.</span><span class="image"> Setting up resource classes</span><br />
</p>
<p class="nb-body">In this panel, you can specify the package and resource names, and the MIME type for your resource. Enter “helloworld” in the Resource Package text field, and in Resource Name enter “HelloWorld”. The Class Name and URI Template fields will default to “HelloWorldResource” and “helloWorld”, respectively. Select text/html for the MIME Type, and click Finish to generate the code.</p>
<p class="nb-body">After the <span class="nb-techtext-bold">HelloWorldResource</span> class is generated, the RESTful Web Services logical view will appear, as shown in <span class="nb-body-bold">Figure&nbsp;4</span>. Double click on the <span class="nb-techtext-bold">getHtml()</span> method node, and the IDE will take you to the method in the Java editor. Modify the method as shown in <span class="nb-body-bold">Listing 1</span>, to return an HTML document displaying “Hello World!”.</p>
<p class="nb-body"><span class="image"><img src="../../../../images_www/magazine/issue04/image18931_opt.jpeg" alt="Figure" /><br />
</span><span class="nb-body-bold">Figure 4.</span><span class="image"> RESTful Web Services Logical View</span><br />
</p>
<table width="100%" border="1" cellpadding="10" cellspacing="0" bordercolor="#869D00">
<tbody>
<tr>
<td bgcolor="#660066"><span class="nb-codelist-title">Listing 1. HelloWorldResource.java</span></td>
</tr>
<tr>
<td valign="top"><pre>@UriTemplate(“helloWorld”)<br />public class HelloWorldResource {<br /> ...<br /> @HttpMethod(“GET”)<br /> @ProduceMime(“text/html”)<br /> public String getHtml() {<br /> return “<br /> &lt;html&gt;<br /> &lt;body&gt;<br /> &lt;h1&gt;Hello World!&lt;/h1&gt;<br /> &lt;/body&gt;<br /> &lt;/html&gt;”;<br /> }<br /> ...<br />}</pre></td>
</tr>
</tbody>
</table>
<p class="nb-body">Taking a closer look at the generated HelloWorldResource.java class shown in <span class="nb-body-bold">Listing 1</span>, notice the <span class="nb-techtext-bold">@UriTemplate(“/helloWorld”) </span>annotation on the class definition. This annotation is what determines the URI for the resource. The <span class="nb-techtext-bold">@HttpMethod(“GET”)</span> and <span class="nb-techtext-bold">@ProduceMime(“text/html”)</span> annotation on the <span class="nb-techtext-bold">getHtml()</span> method indicates to the Jersey runtime that this method should be called when an HTTP GET request is sent to the resource with text/html as the desired MIME type.</p>
<div class="story">
<p class="nb-eye">The HelloWorld project we created in this section is one of the sample applications bundled with the plugin. You can create the sample by choosing <em>File&gt;New Project&gt;Samples&gt;RESTful Web Services&gt;Hello World.</em></p>
</div>
<p class="nb-body">We’ve completed our minimal service. In the next section we demonstrate how to test the service using the Test Client facility.</p>
<p class="nb-section">Testing the service</p>
<p class="nb-body">To test the HelloWorld service, right click on the project node and select Test RESTful Web Services. This action will deploy the application and bring up the test client in your browser, as shown in <span class="nb-body-bold">Figure&nbsp;5</span>. To test the service, click on the helloWorld node on the left-hand panel and click Test. Switch to the Raw View to see the HTML document. The Tabular View extracts all the URIs in the result document, and displays them as links. We will show an example of this later in the article. The Headers view shows all the information in the HTTP headers, and the HTTP Monitor, as expected, displays HTTP request and response messages.<br />
<br />
<span class="image"><img src="../../../../images_www/magazine/issue04/image18937_opt.jpeg" alt="Figure" width="407" height="317" /></span><br />
<span class="nb-body-bold">Figure 5. </span> Test Client for our minimal web service<br />
</p>
<div class="story">
<p class="nb-eye">The Test Client generates its content based on the generated WADL (Web Application Description Language) file which is a description for all the RESTful resources in your project. </p>
</div>
<p class="nb-section">Generating RESTful web services from a database</p>
<p class="nb-body">In addition to generating code from RESTful patterns, you can generate a fully functional RESTful application from a database, leveraging the JPA support in NetBeans. To do this, we will need to generate the JPA entity classes from a database in the web project. </p>
<p class="nb-body">First create a web project called “CustomerDB”, keeping GlassFish V2 as the default server. From the project node, choose New&gt;Entity Classes from Database and select jdbc/sample for Data Source from the drop-down list. A list of tables will appear in the Available Tables column. Select the <span class="nb-techtext-bold">CUSTOMER</span> and <span class="nb-techtext-bold">DISCOUNT_CODE</span> tables and click Add to include them in the Selected Tables column. Click Next to go the panel shown in <span class="nb-body-bold">Figure&nbsp;6</span>. Enter <span class="nb-techtext-bold">customerdb</span> for Package. Next, create a persistence unit by clicking on the Create Persistent Unit button (you can leave everything as default in the dialog and click Create). Click Finish to generate the entity classes. After generation, you should see two Java classes, <span class="nb-techtext-bold">Customer</span> and <span class="nb-techtext-bold">DiscountCode</span>, in the <span class="nb-techtext-bold">customerdb</span> package.</p>
<p class="nb-body"><span class="image"><img src="../../../../images_www/magazine/issue04/image18943_opt.jpeg" alt="Figure" /></span><br />
<span class="nb-body-bold">Figure 6. </span> Entity Class Setup Panel<br />
</p>
<p class="nb-body">You are now ready to generate RESTful web services from the entity classes. Right click on the <span class="nb-techtext-bold">customerdb</span> package node and choose New&gt;RESTful Web Services from Entity Classes to bring up the New RESTful Web Services from Entity Classes wizard as shown in <span class="nb-body-bold">Figure&nbsp;7</span>. In this panel, simply click on Add All to include both entity classes for code generation. Click Next to go the next panel, shown in <span class="nb-body-bold">Figure&nbsp;8</span>.</p>
<p class="nb-body"><span class="image"><img src="../../../../images_www/magazine/issue04/image18949_opt.jpeg" alt="Figure" /></span><br />
<span class="nb-body-bold">Figure 7. </span> Entity Class Selection Panel</p>
<p class="nb-body"><span class="image"><img src="../../../../images_www/magazine/issue04/image18955_opt.jpeg" alt="Figure" /></span><br />
<span class="nb-body-bold">Figure 8. </span> Resource Class Setup Panel</p>
<p class="nb-body">This panel displays a preview of the classes to be generated. The IDE uses the Container-Item pattern to generate the resource classes. For example, for the <span class="nb-techtext-bold">Customer</span> entity class, the IDE generates a container resource called <span class="nb-techtext-bold">CustomersResource</span> and an item resource called <span class="nb-techtext-bold">CustomerResource</span>. Also, for each resource class, NetBeans creates a converter class which is used for generating the resource representation from the corresponding entity instance, e.g. <span class="nb-techtext-bold">CustomersConverter</span> and <span class="nb-techtext-bold">CustomerConverter</span>. Note that there are additional converter classes, e.g. <span class="nb-techtext-bold">CustomerRefConverter</span>, for representing relationships. </p>
<p class="nb-body">Click Finish to generate all the classes indicated in the Preview area. You can test the generated RESTful web service by following the steps outlined in the previous section. <span class="nb-body-bold">Figure&nbsp;9</span> shows an example output. The Tabular View displays a listing of all the URIs embedded in the returned document; you can navigate to them by clicking on each link. </p>
<p class="nb-body"><span class="image"><img src="../../../../images_www/magazine/issue04/image18961_opt.jpeg" alt="Figure" /></span><br />
<span class="nb-body-bold">Figure 9.</span> Testing
RESTful Web Services Generated from Entity Classes<br />
</p>
<p class="nb-body">Taking a closer look at the <span class="nb-techtext-bold">CustomersResource</span> class (shown in <span class="nb-body-bold">Listing 2</span>), notice that the <span class="nb-techtext-bold">getCustomer Resource()</span> method has a <span class="nb-techtext-bold">@UriTemplate(“{customerId}”)</span> annotation. This method is called a “sub-resource locator”, because it is used to return an instance of the <span class="nb-techtext-bold">CustomerResource</span> class to handle the HTTP request, after the Jersey runtime matches the <span class="nb-techtext-bold">customerId</span> in the URI to the pattern specified in <span class="nb-techtext-bold">@UriTemplate</span>. For example, the URI /customers/1 will be matched to this method because /customers is first matched to <span class="nb-techtext-bold">@UriTemplate(“/customers”) </span>in the <span class="nb-techtext-bold">CustomersResource</span> class and “1” will match the <span class="nb-techtext-bold">@UriTemplate(“{customerId}”</span>) annotation for the <span class="nb-techtext-bold">getCustomerResource() </span>method. <br />
</p>
<table width="100%" border="1" cellpadding="10" cellspacing="0" bordercolor="#869D00">
<tbody>
<tr>
<td bgcolor="#660066"><span class="nb-codelist-title">Listing 2. CustomersResource.java </span></td>
</tr>
<tr>
<td valign="top"><pre>@UriTemplate(“/customers/”)<br />public class CustomersResource {<br /> ...<br /> @HttpMethod(“GET”)<br /> @ProduceMime({“application/xml”, “application/json”})<br /> public CustomersConverter get(<br /> @QueryParam(“start”) @DefaultValue(“0”) int start,<br /> @QueryParam(“max”) @DefaultValue(“10”) int max) {<br /> ...<br /> }<br /> ...<br /> @UriTemplate(“{customerId}/”)<br /> public CustomerResource getCustomerResource() {<br /> return new CustomerResource(context);<br /> }<br /> ...<br />} </pre></td>
</tr>
</tbody>
</table>
<p class="nb-body">Also notice in the <span class="nb-techtext-bold">get()</span> method, also shown in <span class="nb-body-bold">Listing 2</span>, that there are two parameters, <span class="nb-techtext-bold">start</span> and <span class="nb-techtext-bold">max</span> with <span class="nb-techtext-bold">@QueryParam</span> annotations on them. These annotations are used by the runtime to inject values of the query parameters specified in a URI into the method parameters. For example, <span class="nb-techtext-bold">/customers?max=20</span> would cause the <span class="nb-techtext-bold">max</span> parameter for the <span class="nb-techtext-bold">get()</span> method to be set to 20 at runtime. The <span class="nb-techtext-bold">@DefaultValue</span> annotation is used to specify default values if no query parameters are specified in the URI. The runtime will convert the value to the type specified by the method parameter.</p>
<p class="nb-body">Now take a look at the <span class="nb-techtext-bold">CustomerResource</span> class shown in <span class="nb-body-bold">Listing 3</span>. Notice the <span class="nb-techtext-bold">@UriParam(“customerId”)</span> annotation for the <span class="nb-techtext-bold">id</span> parameter on the <span class="nb-techtext-bold">get()</span> method. What this annotation does is tell the Jersey runtime to extract the <span class="nb-techtext-bold">customerId</span> from the URI and inject its value into the <span class="nb-techtext-bold">id</span> parameter. The runtime will convert the <span class="nb-techtext-bold">customerId</span> to an <span class="nb-techtext-bold">Integer</span>, as specified by the parameter type.<br />
</p>
<div class="story">
<p class="nb-eye">The generated RESTful web services support both XML and JSON MIME types. You can specify which MIME type to use by selecting the appropriate one from the Method drop-down list.</p>
</div>
<table width="100%" border="1" cellpadding="10" cellspacing="0" bordercolor="#869D00">
<tbody>
<tr>
<td bgcolor="#660066"><span class="nb-codelist-title">Listing 3. CustomerResource.java </span></td>
</tr>
<tr>
<td valign="top"><pre>public class CustomerResource {<br /> ...<br /> @HttpMethod(“GET”)<br /> @ProduceMime({“application/xml”, “application/json”})<br /> public CustomerConverter get(<br /> @UriParam(“customerId”) Integer id) {<br /> ...<br /> }<br /> ...<br />}</pre></td></tr></tbody></table>
<p class="nb-section">RESTful components</p>
<p class="nb-body">In addition to generation of RESTful web services from patterns and JPA entity classes, the plugin supports generating code to access other web services – both RESTful and SOAP-based. This is done using the RESTful components available in the component palette. To bring up the palette with the relevant items, simply open up a RESTful resource class, e.g. <span class="nb-techtext-bold">CustomerResource</span>. <span class="nb-body-bold">Figure&nbsp;10</span> shows the RESTful components currently available. (If the palette does not come up, you can open it with Window|Palette or CTRL+Shift+8.)</p>
<p class="nb-body"><span class="image"><img src="../../../../images_www/magazine/issue04/image18967_opt.jpeg" alt="Figure" /><br />
</span><span class="nb-body-bold">Figure 10.</span> The REST Component Palette<br />
</p>
<div class="story">
<p class="nb-eye">Google Map and Yahoo News Search components are both REST-based; all StrikeIron components are SOAP-based. Currently, you can add components to the palette by creating modules with metadata information in a specific format (the format for such modules is beyond the scope of this article, however). </p>
</div>
<p class="nb-body">To demonstrate how to use these components, drag and drop the Google Map component into your <span class="nb-techtext-bold">CustomerResource</span> class. A dialog will appear (see <span class="nb-body-bold">Figure&nbsp;11</span>), where you can customize the Google Map component. For the apiKey, enter the key which you obtained from Google (by visiting <span class="nb-body-italico">google.com/apis/maps/signup.html</span>). You should also uncheck the Map to QueryParam checkbox for the apiKey, so it does not appear as a query parameter.</p>
<p class="nb-body"><span class="image"><img src="../../../../images_www/magazine/issue04/image18973_opt.jpeg" alt="Figure" /><br />
</span><span class="nb-body-bold">Figure 11.</span><span class="image"> Customizing the GoogleMap Component </span><br />
</p>
<p class="nb-body"> Click Ok and the plugin will generate a sub-resource locator called <span class="nb-techtext-bold">getGoogleMap()</span> in the <span class="nb-techtext-bold">CustomerResource</span> class. The plugin also generates a <span class="nb-techtext-bold">GoogleMapResource</span> class with pre-generated code for invoking the Google Map API. </p>
<p class="nb-body">To have the Google Map keyed to the address of the customer, you need to modify the code as shown in <span class="nb-body-bold">Listing 4</span>. Run Test RESTful Web Services again to test the modified application. <span class="nb-body-bold">Figure&nbsp;12</span> shows the Test Client page. Notice that a new URI, /customers/1/googleMap is added to the <span class="nb-techtext-bold">Customer</span> resource. If you click on the link, you’ll see the map shown in <span class="nb-body-bold">Figure&nbsp;13</span>.</p>
<table width="100%" border="1" cellpadding="10" cellspacing="0" bordercolor="#869D00">
<tbody>
<tr>
<td bgcolor="#660066"><span class="nb-codelist-title">Listing 4. getGoogleMap() method</span></td>
</tr>
<tr>
<td valign="top"><pre> @UriTemplate(“googleMap/”)<br /> public GoogleMapResource getGoogleMap(<br /> @UriParam(“customerId”) Integer id)<br /> {<br /> try {<br /> x.Customer entity = getEntity(id);<br /> String apiKey = null;<br /> String address = entity.getAddressline1() + “ “ +<br /> entity.getAddressline2() + “ “ +<br /> entity.getCity() + “ “ +<br /> entity.getState() + “ “ +<br /> entity.getZip();<br /> Integer zoom = null;<br /> return new GoogleMapResource(apiKey, address, zoom);<br /> } finally {<br /> PersistenceService.getInstance().close();<br /> }<br /> }</pre></td>
</tr>
</tbody>
</table>
<p class="nb-body"><img src="../../../../images_www/magazine/issue04/image18979_opt.jpeg" alt="Figure" /><br />
<span class="nb-body-bold">Figure 12.</span> Testing the GoogleMap Resource</p>
<p class="nb-body"><img src="../../../../images_www/magazine/issue04/image18985_opt.jpeg" alt="Figure" /><br />
<span class="nb-body-bold"> Figure 13.</span> Google Map for Customer 1</p>
<p class="nb-section">Client library generation</p>
<p class="nb-body">To facilitate building client applications that access RESTful web services, the plugin supports generation of client libraries written in JavaScript. The generated libraries can contain three layers of abstraction. At the bottom layer is plain JavaScript code which can be consumed by JavaScript clients. The next layer provides support for the Dojo Ajax toolkit. The generated code is organized in a structure understood by Dojo and contains code for the Dojo store and widget abstractions. On the top is the jMaki layer, which is a wrapper around the Dojo layer. </p>
<p class="nb-body">In order to generate all three layers, you need to first install the jMaki plugin available in the download area of the ajax.dev.java.net website. This plugin will automatically install the Dojo libraries so you do not need to install these separately. (If you do not install the jMaki plugin, you’ll only be able to generate plain JavaScript code.)</p>
<p class="nb-body">To create the client library, you need to first create a web project, say “CustomerDBClient”. Also, make sure you add the jMaki framework as shown in <span class="nb-body-bold">Figure&nbsp;14</span>. Next, right click on the project node and invoke <span class="nb-body-italico">New&gt;RESTful Web Service Client Stubs</span>, to invoke the wizard shown in <span class="nb-body-bold">Figure&nbsp;15</span>.</p>
<p class="nb-body"><span class="image"><img src="../../../../images_www/magazine/issue04/image18991_opt.jpeg" alt="Figure" /></span><br />
<span class="nb-body-bold">Figure 14.</span> Adding the jMaki Framework</p>
<p class="nb-body"><span class="image"><img src="../../../../images_www/magazine/issue04/image18997_opt.jpeg" alt="Figure" /></span><br />
<span class="nb-body-bold">Figure 15.</span> New RESful Web Service Client Stubs Wizard</p>
<p class="nb-body">Click on Add Project and select the CustomerDB project you created. Make sure the Create JMaki Rest Components checkbox is selected and click Finish. The plugin will generate all the necessary files in the project’s web folder. </p>
<p class="nb-body">You can test the generated client library by running the TestResourcesTable.jsp page, in the <span class="nb-body-italico">web/resources/dojo/rest</span> folder (simply right click on the file and choose Run File). <span class="nb-body-bold">Figure&nbsp;16</span> shows the result.</p>
<p class="nb-body"><span class="image"><a href="../../../../images_www/magazine/issue04/image19003_opt-small.jpg"><img src="../../../../images_www/magazine/issue04/image19003_opt-small.jpg" style="border: 0px;" alt="Figure (click for full size)" /></a></span><br />
<span class="nb-body-bold">Figure 16. </span>Example using jMaki </p>
<p class="nb-body">You can also install the generated client library as a jMaki component in the jMaki palette. In the root directory for the CustomerDBClient project, there should be a CustomerDB.zip file already created when you create the client library. You can see it in the Files view of the IDE. Next, choose Tools&gt;Palette&gt;Add jMaki Library and select CustomerDB.zip. The jMaki plugin will add the jMaki components contained in this file to the jMaki palette, as shown in <span class="nb-body-bold">Figure&nbsp;17</span>. Finally, to test the component, simply drag and drop it into a JSP file such as index.jsp, and run it from the IDE.</p>
<p class="nb-body"><span class="image"><img src="../../../../images_www/magazine/issue04/image19009_opt.jpeg" alt="Figure" /></span><br />
<span class="nb-body-bold">Figure 17.</span> jMaki palette</p>
<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#000033">
<tbody>
<tr>
<td bgcolor="#000033"><p class="nb-table-title">REST Patterns</p></td>
</tr>
<tr align="left">
<td valign="top" bgcolor="#869D00"><div class="story">
<p class="nb-table-body1">The RESTful plugin currently supports three patterns. The Singleton pattern is useful for creating RESTful wrapper services around other services such as SOAP-based web services. For example, you can use this pattern to REST-enable existing SOAP-based services. This pattern has many additional uses. </p>
<p class="nb-table-body1">The Container-Item pattern is typically used for representing resources that have relationships, such as a database. We use this pattern to generate code from JPA entity classes. </p>
<p class="nb-table-body1">This “plain” Container-Item pattern lets you create an item resource using the POST method. The URI for the newly created item resource is determined by the container resource. </p>
<p class="nb-table-body1">The Client-Controlled Container-Item pattern is a slight variation. The difference is that there is no POST method on the container resource class for creating item resources. Instead, item resources are created using PUT on the item resource class. It’s called Client-Controlled because the URI for the item resource is determined by the client and not by the container resource. Amazon’s Simple Storage Service (S3) uses the Client-Controlled Container-Item pattern.</p>
</div></td>
</tr>
</tbody>
</table>
<p class="nb-section">Conclusions</p>
<p class="nb-body">In this article, we gave you an introduction to the world of RESTful web services, and the current efforts by JAX-RS and Jersey to standardize on a Java API for building such web services. We also showcased the RESTful Web Service plugin for NetBeans 6.0, which provides end-to-end support for building complete RESTful web services.</p>
</div>
<div class="image"></div>
<div class="story"></div>
<div class="image"></div>
<div class="image"></div>
<div class="image"></div>
<div class="image"></div>
<div class="story"></div>
<div class="image">
<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#869D00">
<tbody>
<tr>
<td height="30" bgcolor="#000033"><p class="nb-table-title">Links</p></td>
</tr>
<tr>
<td valign="top"><div class="group">
<div class="story">
<p class="nb-link-legend-url1"><a href="http://jcp.org/en/jsr/detail?id=311">http://jcp.org/en/jsr/detail?id=311</a></p>
</div>
<div class="story">
<p class="nb-link-legend">JSR 311 – JAX-RS: The Java API for RESTful Web Services </p>
</div>
</div>
<div class="group">
<div class="story">
<p class="nb-link-legend-url1"><a href="http://jersey.dev.java.net">http://jersey.dev.java.net/</a></p>
</div>
<div class="story">
<p class="nb-link-legend">Jersey, the open source JAX-RS (JSR 311) Reference Implementation for building RESTful Web services</p>
</div>
</div>
<div class="group">
<div class="story">
<p class="nb-link-legend-url1"><a href="http://ajax.dev.java.net">http://ajax.dev.java.net/</a></p>
</div>
<div class="story">
<p class="nb-link-legend">Prokect jMaki</p>
</div>
</div>
<div class="group">
<div class="story">
<p class="nb-link-legend-url1"><a href="http://wadl.dev.java.net">http://wadl.dev.java.net/</a></p>
</div>
<div class="story"><p class="nb-link-legend">Web Application Description Language project</p>
</div>
</div></td>
</tr>
</tbody>
</table>
<br />
</div>
<div class="image"></div>
<div class="image"></div>
<div class="nb-figura-t-tulo"></div>
<div class="image">
<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#869D00">
<tr>
<td width="10%"><img src="../../../../images_www/magazine/issue04/peter_liu_opt.jpeg" alt="Peter Liu" width="97" height="102" /></td>
<td width="90%"><div class="story">
<p class="nb-minibio"><span class="nb-autor-name">Peter Liu </span>is a staff engineer at Sun Microsystems, Inc. <br />
leading the RESTful Web Services tooling effort for <br />
NetBeans 6.0.</p>
</div></td>
</tr>
</table>
</div>
</div>
</body>
</html>