This tutorial demonstrates building RESTful Web Services in NetBeans 7.
Approximately 30 minutes.
Representational State Transfer (REST), is a Web Service model is a simpler alternative to SOAP and Web Services Description Language (WSDL) based Web Services. Building Web Services using the RESTful approach is emerging as a popular Web Service model due to its lightweight nature.
RESTful Web Services enable message exchange over HTTP using formats like -XML, JSON 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. Exposing a system's resources through a RESTful API is a flexible way to integrate applications.
REST is just an architectural style, not a technology. Java specification (JSR 311) describes how REST should be implemented in Java.
There have been several implementations of this standard. Jersey is its official reference implementation and the one that is most widely used in development and production.
NetBeans 7.1 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. The following are the RESTful features provided in NetBeans :
In this tutorial, you will
The following software is required to complete this tutorial in Windows
platform. You must install the software in the given order.
Java DB database server is part of NetBeans. We will use Java DB as the database server. The following steps demonstrate creating the database playerDB.
. |
To start the Java DB Database from NetBeans, perform the following steps. a. Click Services tab.
Note the following output in the Output window, indicating that the DB server has started: Note that the DBserver version could vary from the version shown in the screenshot depending on the JDK build updates. |
---|---|
. |
To create playerDB database, perform the following steps:
a. Right-click Java DB icon, select Create Database.
b. Enter the following information for the database:
c. Click OK.
This creates the database and adds a connection for the database under
the Databases icon.
|
. |
To connect to the newly created database playerDB, perform the following steps : a. Right-click jdbc:derby://localhost:1527/playerDB connection. b. Select Connect. |
. |
Create tables and populate them with data in playerDB database. a. In NetBeans select File > Open File. b. In the file browser navigate to the directory, where you unzipped the files from the Prerequisites section and select playersDB.sql. c. Click Open. The script automatically opens in the SQL Editor.
d. Select jdbc:derby://localhost:1527/playerDB in Connection drop-down box in the SQL Editor toolbar. e. Click the Run SQL icon to execute the SQL statement.
|
. |
Examine the contents of the database. a. In the Services window, expand the jdbc:derby://localhost:1527/playerDB
connection under the Databases node.
d. Expand the Tables node to see the PLAYER and TEAM tables.
An SQL command window opens and executes an SQL command to display the data in the table. f. Repeat the previous step for the TEAM table. |
To create RESTful Web Services, you need a Java Web application project. In the below section you will create a demo Java web project, PlayerServer.
. |
To create new Java Web Project, select File > New Project.
|
---|---|
. |
Select Java Web from the Categories column and Web Application from the Projects column and click Next.
|
. |
Perform the following steps: a. Name the project PlayerServer.
|
. |
In the Server and Settings window, verify GlassFish Server is selected as Server and click Finish.
|
. |
To start the Server, perform the following steps : a. Open the Projects tab. b. Right-click PlayerServer project. c. Select Run. This action starts the GlassFish server and deploys the application. On successful deployment of the application - a default jsp page with url, http://localhost:8080/PlayerServer/ is opened in the browser displaying "Hello World".
|
Creation of RESTful Web Services in Java depends on the Java Persistence API to communicate with a database. You can use NetBeans IDE either to create entity classes and RESTful Web Services in the same process, or you can use the IDE to create RESTful Web Services from existing entity classes.
The below section demonstrates how to use the RESTful Services from Database wizard to generate entity classes and RESTful Web Services in the same process.
|
To generate RESTful Web Services do the following: Right-click the PlayerServer and choose New > Other > Web Services > RESTful Web Services from Database. The New RESTful Web Service wizard opens on the Database Tables panel. |
---|---|
. |
In the Database Tables window, select Data
Source. |
. |
Next select "New Data Source" from the Drop-down list. a. In the Create Data Source Window, enter the following information:
b. Click OK.
The PLAYER and TEAM tables are displayed under the Available Tables column. c. Click Add All. The PLAYER and TEAM tables are displayed under the Selected Tables column.
d. Click Next.
|
. |
In the Entity Classes window, complete the following steps: a. Enter the package name as com.playerentity. b. Click Next.
|
. |
In the Generated Classes Window, click Finish with default values.
|
. |
In the Rest Resources Configuration Window, click OK.
|
. |
In the Projects pane, perform the below steps : a. Select and expand the PlayerServer project. b. Expand the source packages of the project. c. Expand the com.playerentity package. d. Verify the creation of Entity Classes Player.java and Team.java. e. Expand the RESTful Web Services folder and and verify the creation of two RESTful Web Services PlayerFacadeREST[com.playerentity.player] and TeamFacadeREST[com.playerentity.team].
The above generated two Web Services are the REST front end of our application. For each entity, there is a resource that lists all the entity's instances. |
The following section demonstrates how to test RESTful Web Services with tests that are generated using the test framework provided by Jersey.
. |
To Generate Web Services Test client, complete the following steps. Configure REST test Client window opens as shown in the below screenshot. |
---|---|
. |
Select "Web Test Client in project" and click Browse.
|
. |
a. In the Select Project dialog box, select PlayerServer and click OK. b. Configure Rest Test Client window is displayed, click OK. The server starts and the application is deployed. When deployment is complete, the browser displays your application, with a link for each of the Web Services.
On the left-hand side is the set of root resources named com.playerentity.team and com.playerentity.player. |
. |
To test the Web Services Client, complete the following steps. a. Select one resource node such as com.playerentity.team.b. In the "Choose method to test" field, select either GET (application/json) or GET (application/xml). c. Click Test.
The test client sends a request and displays the result in the Test Output section. The test client displays the Raw View by default. d. Examine the output: Response to an application/xml request. Response to an application/json request. JSON is widely used in REST-based applications, because this format is more compact than XML. Also it can easily be used with most common programming languages, including JavaScript. e. Likewise select the com.playerentity.player node and test the Web Service. |
This tutorial provides an introduction to RESTful Web Services. You learned how NetBeans allows you to qiuckly develop and test REST-based applications.
Credits
Copyright © 2011, Oracle and/or its affiliates. All rights reserved | |