Create RESTful Web Services in Netbeans


Author:Gao Ang


Representational
state transfer (REST) is a style of software architecture for distributed
hypermedia systems. The terms “representational state transfer” and “REST” were
introduced in 2000 in
the doctoral dissertation of Roy Fielding, who is one of the principal authors
of the Hypertext Transfer Protocol(HTTP) specification.


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. REST is not strictly only a method of building web services. 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.


The most useful
HTTP methods are POST, GET, PUT and DELETE. These
are often compared with the CREATE, READ, UPDATE, DELETE (CRUD) operations
associated with database technologies.


The following
table associates several common HTTP verbs with similar database operations,
however the meaning of the HTTP verbs do not correspond directly with a single
database operation. For example, an HTTP PUT is used to set the value of a
resource and may result in either a creation or replacement as needed.



HTTP CRUD


POST Create,
Update, Delete


GET Read


PUT Create,
Overwrite/Replace


DELETE Delete


REST emphasis on the diversity of resources, we could understand the
meaning of the operation when we see the URL of the link. For example, a REST
application might define the following resources:


  • http://example.com/users/

  • http://example.com/users/{user} (one for each user)

  • http://example.com/findUserForm

  • http://example.com/locations/

  • http://example.com/locations/{location} (one for each location)

  • http://example.com/findLocationForm



For Ruby on Rails, RESTful web services are the default style in the scaffold
generation of the framework. Now, with Netbeans, we could create a RESTful web
services application in Java from scratch easily.


NetBeans 6.x supports the rapid development of RESTful web services using
JSR-311 (Java API for RESTful Web Services – JAX-RS), the reference
implementation for JAX-RS. NetBeans IDE supports building and testing services,
as well as creating client applications that access these services, and
generating code for invoking web services (both RESTful and SOAP-based).


Here are some highlights of RESTful Web Services in NetBeans:



  • Rapid creation of
    RESTful web services from Java Persistence API entity classes and patterns.

  • Fast code
    generation for invoking web services such as Google Maps, Yahoo News Search by
    drag-and-dropping components from the RESTful component palette.

  • Generation of JavaScript
    client stubs from RESTful web services for building client applications.

  • Test client
    generation for testing RESTful web services.

  • A logical view for
    easy navigation of RESTful web service implementation classes in project.



To follow this tutorial, we need the following software and resources.



  • NetBeans IDE 6.x. (I
    use the NetBeans 6.1 version)

  • JDK version 5 or 6

  • GlassFish V2
    Application Server



Besides, we need
the RESTful web services plugin to implement our demo code. Fortunately, we get
this plugin installed by default if we choose the NetBeans web development
edition.




First, let’s create
a new project in Netbeans. We will select Web Application in Web Categories and
click Next.





In the next step, we name the project as “RestDemo”. Leave the rest as default and
step into the next view:


GlassFish is an application server project by Sun Microsystems for the Java
Enterprise Edition (Java EE) platform. We use GlassFish V2 as the server of our
web services, and RestDemo as the context path and then click Finish.





Based on our RestDemo
project, we are gonna create Web Resource for the web application.




Singleton pattern
is a design pattern that is used to restrict instantiation of a class to one
object. We use Singleton Pattern to create a simple Java Class for our web
resource like this:




We should specify Resource
Name and other required for the Resource class and finish this procedure. We
use text/html MIME Type to display the response in HTML format.



Now, we get the
generated HTTP methods and the tree view of our project looks like:




Besides, we get
the generated source code in DemoResource.java, before we start up the web
server. Let’s add some code to return sample HTML message in getHtml method, which
looks like:




Then we use Glassfish
to test the RESTful Web Services, Glassfish configuration looks like:





Right click on the RestDemo project node and select “Test RESTful Web
Services”option to start server. GlassFish V2 application server will start and
jump to our deployed web application.

/\*




In the web explorer, we could see a link for the web
service that we just created, choose the GET(text/html) method to test our
service.





The result will show up in the Http monitor. We get the
html text in the received parameter like this:




Besides, we could also generate RESTful web services from database and generate
CRUD operation in RESTful style for the database record with the help of powerful
Netbeans IDE.





Reference


http://www.netbeans.org/community/magazine/html/04/restfulws.html


http://www.testearly.com/2008/07/23/restful-web-services-in-60-seconds/
http://en.wikipedia.org/wiki/REST


http://zh.wikipedia.org/w/index.php?title=REST&variant=zh-cn

评论:

发表一条评论:
  • HTML语法: 禁用
About

This is Chinese Functional CA Team Blog. Any suggestion is appreciated.

Search

Archives
« 四月 2014
星期日星期一星期二星期三星期四星期五星期六
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
今天