REpresentational State Transfer (REST) is an architectural style for distributed hypermedia systems, such as the World Wide Web. Central to the RESTful architecture is the concept of resources identified by universal resource identifiers (URIs). These resources can be manipulated using a standard interface, such as HTTP, and information is exchanged using representations of these resources.
Contents |
Before you begin, you need to download and install the following software on your computer:
RESTful web services are services built using the RESTful architectural style. 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 and the ability to transmit data directly over HTTP.
The IDE supports 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.
For detailed information, refer to the following:
In addition to building RESTful web services, the IDE also supports testing, building client applications that access RESTful web services, and generating code for invoking web services (both RESTful and SOAP-based.)
Here is the list of RESTful features provided by the IDE:
In this tutorial, you will be shown how the IDE supports you in generating, implementing, and testing RESTful web services.
The goal of this exercise is to create a project and generate entity classes from a database.
Alternatively, you can select it from the Persistence category in the New File wizard, shown below:
Click Next.
Click Next.
Click Create and then click Finish.
The goal of this exercise is to generate RESTful web services from the entity classes that we generated in the previous section.
Click Next.
Here you can see everything that the IDE will generate for you. The IDE uses the container-item pattern to generate the resource classes. For example, for the Customer entity class, the IDE generates a container resource called CustomersResource and an item resource called CustomerResource. Also, for each resource class, the IDE generates a converter class used for generating the resource representation from the corresponding entity instance, such as CustomersConverter and CustomerConverter. Furthermore, there is an additional converter class called reference converter, such as CustomerRefConverter, for representing relationships.
Click Finish.
The RESTful Web Services node in the Projects window displays all the RESTful web services in your project. The value between the square brackets, such as [/customers/], is the value for the URI template. You can also navigate to the source file by doubling click on this node. This view also displays all the HTTP methods and Sub-resource locator methods. Again, you can navigate to the methods by double clicking on the nodes.
Now that our entity classes and RESTful web services have been generated, let's test our application. The IDE provides a useful utility for testing RESTful web services. We will make use of it in the next section.
The goal of this exercise is to try out our application.
The server starts and the application is deployed.
Finally, the browser should display your application, with a link for
each of the web services:
On the left-hand side is the set of root resources, here they are named customers and discountCodes.
There are 4 tabs in the Test Output section. The Tabular View is a flattened view that displays all the URIs in the resulting document, which you can navigate to by clicking on the links. The Raw View displays the actual data returned. Depending on which mime type you selected (application/xml or application/json), the data displayed will be in either XML or JSON format, respectively. The Headers Tab displays the HTTP header information. There is also a tab called HTTP Monitor, which displays the actual HTTP requests and responses sent and received.
The goal of this exercise is to add Google map functionality to our RESTful web services.
@UriTemplate("googleMap/") public GoogleMapResource getGoogleMapResource() { String address = "16 Network Circle, Menlo Park, CA"; String mapKey = "abcdef"; String httpProxy = ""; return new GoogleMapResource(address, mapKey, httpProxy); }
Change it to the following:
@UriTemplate("googleMap/") public GoogleMapResource getGoogleMapResource(@UriParam("customerId") Integer id) { Customer c = getEntity(id); String address = c.getAddressline1() + " " + c.getAddressline2() + " " + c.getCity() + " " + c.getState() + " " + c.getZip(); String mapKey = "abcdef"; String httpProxy = ""; return new GoogleMapResource(address, mapKey, httpProxy); }
The code extracts the address information from the customer entity and passes it to the GoogleMapResource instance.
For more information about using NetBeans IDE 6.0 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 [email protected] mailing list.