Sunteți pe pagina 1din 14

testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

Getting Started with RESTful Web Services


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. In this tutorial, you first learn a bit about REST and then you are shown how NetBeans
IDE 6.0 supports this architectural style.

Contents
Introduction
Generating Entity Classes from a Database
Generating RESTful Web Services from Entity Classes
Testing the RESTful Web Services
Adding a Google Map Feature

Software Needed for the Tutorial


Before you begin, you need to download and install the following software on your computer:

NetBeans IDE 6.0 Web & Java EE (download).


Java Standard Development Kit (JDK) version 5.0 or above (download).
Glassfish V2 or Tomcat Web Server, both of which you can select from the installer that you use to install
NetBeans IDE 6.0.
In the IDE, go to the Plugin Manager, under the Tools menu, and install the RESTful Web Services plugin, as
shown below:

Introduction
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:

JSR 311: JAX-RS: The Java API for RESTful Web Services
Jersey, the open source JAX-RS (JSR 311) Reference Implementation for building RESTful Web services
In addition to building RESTful web services, the IDE also supports testing, building client applications that access RESTful

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 1 of 14
testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

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:

1. Rapid creation of RESTful web services from JPA entity classes and patterns.

2. Rapid code generation for invoking web services such as Google Map, Yahoo News Search, and StrikeIron web
services by drag-and-dropping components from the RESTful component palette.
3. Generation of JavaScript client stubs from RESTful web services for building RESTful client applications.
4. Test client generation for testing RESTful web services.

5. Logical view for easy navigation of RESTful web service implementation classes in your project.

In this tutorial, you will be shown how the IDE supports you in generating, implementing, and testing RESTful web
services.

Generating Entity Classes from a Database


The goal of this exercise is to create a project and generate entity classes from a database.

1. Choose File > New Project. Under Categories, select Web. Under Projects, select Web Application and click Next.
2. Under Project Name, enter C u s to m e r D B .

3. Under Server, select a server, such as GlassFish. Click Finish.

4. Right-click the C u s to me r D B node and choose New > Entity Classes from Database, as shown below:

Alternatively, you can select it from the Persistence category in the New File wizard (Ctrl-N), shown below:

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 2 of 14
testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

Click Next.

5. In the Database Tables panel, select the j d bc/ s amp le data source. Next, under Available Tables, select CUSTOMER
and then click Add. The DISCOUNT_CODE table, which has a relationship with the CUSTOMER table, is automatically
added to the Selected Tables list too. You should now see the following:

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 3 of 14
testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

Click Next.

6. Under Package, type c u s t o m e r db . You should now see the following:

7. Click Create Persistence Unit. The following dialog appears:

Click Create and then click Finish.

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 4 of 14
testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

8. Look in the Projects window. You should now see the following:

Generating RESTful Web Services from Entity Classes


The goal of this exercise is to generate RESTful web services from the entity classes that we generated in the previous
section.

1. Right-click the package that contains the entity classes and choose New > RESTful Web Services from Entity
Classes, as shown below:

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 5 of 14
testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

2. In the New RESTful Web Services from Entity Classes wizard, click Add All. You should now see the following:

Click Next.

3. In the next panel, leave the defaults unchanged, as shown below:

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 6 of 14
testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

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.

4. Look in the Projects window. You should now show the following:

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 7 of 14
testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

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 [ /c u sto me rs /] , is the value for the URI template. You can also
navigate to the source file by doubling clicking 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.

Testing the RESTful Web Services


The goal of this exercise is to try out our application.

1. Right-click the project node and choose Test RESTful Web Services, as shown below:

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 8 of 14
testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

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 cus to me rs and di sc oun tC od e s .

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 9 of 14
testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

2. First select a root resource, such as c u s t o m ers . Click on the Methods drop-down list to select either
GE T ( a pp l i c at i o n/ xm l ) or G E T (a p p l i c a t ion / jso n) . Click Test. The result is displayed in the Test Output section:

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 (a pp lic at io n/ xml or ap pl ica ti on /j son ), 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.

3. Exit the browser and return to the IDE.

Adding a Google Map Feature


The goal of this exercise is to add Google map functionality to our RESTful web services.

1. Open the C u st o m er Re s o u r c e class in the editor, as shown below. Under the Tools menu, choose Palette. Notice
that a palette appears on the right side of the editor, containing code snippets:

2. Sign up for a Google map key at http://www.google.com/apis/maps/signup.html.


3. From the Google category, drag the Map item and drop it anywhere in the class, and enter the Google Map key into
the "apiKey" field, as shown here:

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 10 of 14
testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

Click OK.

The IDE creates the G o o g l e M a p Re s o u r c e class. The C us to mer Re so ur ce class accesses it using this method, that
the IDE also created:

/* *
* R et u r n s G o o g l e M a p Re s o u r c e s ub- r eso ur ce .
*/
@U r i Te m p l a te ( " g o o g l e Ma p / " )
pu b l ic G o o gl e M a p R e s o ur c e g e t G o ogl e Map (
@ U ri P a r a m ( " c us t o m e r I d " ) I n teg er i d) {
tr y {
c u st o m e r d b . C us t o m e r e n tit y = ge tE nt ity (i d) ;
S t ri n g a p i K e y = n u l l ;
S t ri n g a d d r e ss = e n t i t y.g e tAd dr es sl ine 1( );
I n te g e r z o o m = n u l l ;
r e tu r n n e w G oo g l e M a p R e sou r ce( ap iK ey , a dd re ss , z oo m) ;
} f i n a ll y {
P e rs i s t e n c e S er v i c e . g e t Ins t anc e( ). cl ose () ;
}
}

4. Right-click the project node and choose Test RESTful Web Services again and, once the browser opens again, click
go o g l eM a p . You should see the following, with the ap iK ey filled in:

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 11 of 14
testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

5. Click the Test button. Notice that the Google map opens in the Raw View tab:

6. Whenever you click a Google map link, the same address will be shown. That is because the address is hard coded
in the Go o g l eM a p R es ou r c e class. We will now modify the generated method that we looked at earlier in the
Cu s t o me r R e so u r ce class:

@U r i Te m p la t e ( " go o g l e M a p / ")
pu b l ic G oo g l e M ap R e s o u r c e g e t G o o g l e Map ( @Ur iP ar am ("c us to me rId ")
In t e ge r id ) {
tr y {
cu s t o m er d b . C u s t o me r e n t i t y = g etE nt it y( id) ;
St r i n g a p i K e y = nu l l ;
St r i n g a d d r e s s = e n t i t y . g e tAd d res sl in e1 ();
In t e g e r z o o m = n ul l ;
re t u r n n e w G o o g l eM a p R e s o u r ce( a piK ey , ad dre ss , zo om) ;

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 12 of 14
testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

} f i na l l y {
Pe r s i s te n c e S e r v i ce . g e t I n s t anc e (). cl os e( );
}
}

Change it to the following:

@U r i Te m p la t e ( " go o g l e M a p / ")
pu b l ic G oo g l e M ap R e s o u r c e g e t G o o g l e Map R eso ur ce (
@U r i P a ra m ( " c u s t o me r I d " ) I n teg e r i d) {
Cu s t om e r c = g e t E n t i ty ( i d ) ;
St r i ng a d d re s s = c . g et A d d r e s s l ine 1 () + " " + c .g et Ad dre ss li ne 2() +
" " + c . g e t C it y ( ) + " " + c.g et St at e() + " " + c. ge tZ ip( );
St r i ng m a p Ke y = " a b c de f " ;
In t e ge r z o om = n u l l ;
re t u rn n e w G o o g l e M a p Re s o u r c e ( m apK e y, ad dr es s, zo om );
}

The code extracts the address information from the customer entity and passes it to the GoogleMapResource
instance.

7. Test the application again. Click cu s t o m e r s in the left sidebar, instead of go og le Map . Choose an Input Method.
Click Test. Click / c us to m e r s / 1 / . Click Test. Now you have two links, one for discount code and one for Google
Map. Click the Google Map link and then click Test again. Notice that the correct Google map entry is now returned
for the customer:

Send Us Your Feedback

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

Getting Started with JAX-WS Web Service Clients


Getting Started with JAX-WS Web Services
Getting Started with RESTful Web Services
Consuming StrikeIron Web Services in Web Applications
Consuming Google Web Services in Web Applications
Advanced Web Service Interoperability

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 13 of 14
testwww: Getting Started with RESTful Web Services 12/11/07 9:45 PM

Java EE Applications Learning Trail


YouTube: RESTful Web Services, Building and Deploying (Part 1)
YouTube: NetBeans RESTful Testing and Invoking RESTful Resources (Part 2)
To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java
EE development features, join the nbj2ee@netbeans.org mailing list.

http://testwww.netbeans.org/kb/60/websvc/rest.html#Exercise_0 Page 14 of 14

S-ar putea să vă placă și