RESTful Web Services

advertisement
RESTful Web Services
by
Senthil Chinnaiyan, Senior Architect
Chandra Ramachandran, Architect
29–Oct–2010
Agenda
• Part I






Introduction
What is REST?
How does it work?
RESTful Architecture
REST vs SOAP
REST – Why and When?
• Part II

JAX-RS


Jersey
RESTful Service – Deep dive


Designing Service
Implementing Service
• Q&A
2
Part I
REST Concepts
3
Introduction
• In this modern world, enterprises are in the need of implementing Service
Oriented Architecture (SOA) to achieve maximum business benefits
• SOAP/WSDL/UDDI - SOA stack is the most common way and have failed to
live up to their promise in some cases
• Due to the complexity and high cost, enterprises are looking at alternatives to
build their integration architecture in a simpler way.
• In order to achieve low-cost, flexible integration, increased data security, and
greater scalability, there is tremendous interest in REpresentational State
Transfer (REST) architectural style
4
What is REST?
• REST stands for “REpresentational State Transfer”, was introduced and
defined in 2000 by the doctoral dissertation of Roy Fielding
• REST is an architectural style which has set of constraints that can be applied
on networking architecture to create RESTful architecture typically, on client
server architecture.
HTTP Request
XML, JSON, HTML,
TEXT etc
HTTP Response
Browser
HTTP Request
XML, JSON, HTML,
TEXT etc
HTTP Response
Application
Application
5
REST
REST Server
Server
(Web
(Web Services)
Services)
How does REST work? …
• Resources can be anything,
simple entity or entities
• Hyperlink to a resource and the
only way of exchange data
between client and server
• Actual data of the system and
can be in any form
• Each resource can be accessed
by unique ID called URI
• Example: Customer, Orders etc
Resources
• Representation is a temporal
state of the actual data
• Example:
<domain>/depts/
<domain>/depts/finance
<domain>/employees/senthil
• Representation is sent back and
forth between client and server
• Example: XML, JSON, Text
Text
Representation
URI
… How does it work?
REST in action
• In REST URIs are used to connect clients and servers to exchange resources
in the form of representations
• In order to exchange data, REST relies on basic HTTP protocol methods:
GET, POST, PUT and DELETE.
2
3
GET /emp/Senthil
• 1 Accept header is
used for representation
RETRIEVE
HTTP/1.1
Accept: text/xml
1
GET
...
POST /emp/Senthil
HTTP/1.1
Accept: text/xml
Content-Type:
text/xml
<xml>… </xml>
INSERT
POST
• 2 HTTP method is
used for operation
•3
URI is used to
locate resources
PUT /emp/Senthil
UPDATE
PUT
HTTP/1.1
Accept: text/xml
Content-Type:
text/xml
<xml>… </xml>
7
DELETE /emp/Senthil
HTTP/1.1
Accept: text/xml
Content-Type:
text/xml
<xml>… </xml>
DELETE
DELETE
… How does it work?
REST in detail (GET/RETRIEVE)
• The method GET is used to RETRIEVE resources.
• Client application makes a HTTP request with the method type GET and
Senthil as the identifier
GET /employees/Senthil
HTTP/1.1
Host: localhost
Date: Sat, 23 Oct 2010
Accept: text/xml
• The representation type is set
through the Accept request header
Servlet Container
...
Request
• REST Framework invokes http://localhost:8080/restService/api/employees/senthil
domain code to retrieve
HTTP/1.1 200 OK
Date: Sat, 23 Oct 2010
data and to generate
Server: Apache/1.3.3.7
Client
Application
Content-Length: 438
Representation in XML
Connection: close
Content-Type: text/xml;
charset=UTF-8
<xml>
<emp>
<fname>Senthil</fname>
<lname>Chinnaiyan</lname>
</emp>
</xml>
Response
• Servlet container sends back the
response as XML with 200 as
the status code
8
REST Framework
(REST Constraints)
Domain Code
(Business Logic)
RESTful Service
RESTful Architecture
• A typical Java based RESTful architecture consists of 3 layers.
• The Restful Service layer intercepts incoming http requests to identify the
representation, resource and the operation
• JAX-RS is the
specification defined by
Sun to create and use
RESTful services in
Java applications.
http
Client
Application
RESTful Service Layer
(JAX-RS)
Business Layer
(Spring/Seam/EJB...)
• The Business Layer uses appropriate
framework to execute the business logic.
• The Data Layer is responsible for
data logic and takes care of O/R mapping.
9
Data Layer
(Hibernate/iBatis/EJB...)
jdbc
RESTful Architecture
Database
REST vs SOAP?
• Lightweight – easy to develop, no toolkits are required, works based on
fundamental principles of HTTP and no contracts involved.
• SOAP is complex, it requires greater implementation effort and understanding
on the client side and server side.
• The XML metadata information and SOAP headers associated with web
services are considered to be an overhead than the HTTP headers.
• HTTP itself is providing all facilities for an efficient, secured consumption of
business logic and data, so SOAP and WS-* are considered overhead.
• The stateless, highly scalable RESTful services are adopted in major key
players such as Google, Amazon, Microsoft, Digg, Flickr and Twitter
10
Why and When?
Consider REST:
• REST may be considered if the service model is light, stateless, low cost,
which requires improved data security and high scalability
Consider SOAP:
• REST does not enforce any service contract, if the service model requires
stronger service contract between server and client, REST may not be an
option.
• Some of the SOAP capabilities such as transaction, policy are not
standardized in REST
• REST operates on stateless model, which may not be friendly for stateful
service model
11
Part II
REST Deep Dive
12
JAX-RS
JAX-RS (Java API for RESTful Web Service)
• JAX-RS provides support in creating Web Services according to the REST
architectural style.
• Simple, Annotation based, introduced in Java SE5 for the development and
deployment of web services and clients.
Annotations
Description
@Path
Relative path for a resource class
@GET, @PUT, @POST, @DELETE
HTTP request type of a method
@Produces
Returned MIME media types
@Consumes
Acceptable request media types
@Param** (Ex: @PathParam)
Parameter values
• Some of the Implementations of JAX-RS:
• Apache CXF, Sun’s Jersey, Jboss’s RESTEasy, RESTlet, Apache Wink
13
Jersey
• Jersey is a reference implementation of JAX-RS by Sun Microsystems.
• Jersey implements all the REST constraints as defined by Roy.
• Jersey implements support for the annotations defined in JSR-311, making it
easy for developers to build RESTful web services with Java and the Java
JVM.
• Uses JAXB for XML to Objects Binding
• Integrates well with Spring framework
• Supports WADL (Web Application Description Language). WADL is the REST
equivalent for WSDL
14
REST – How to? …
RESTful web service life cycle can be summarized in the following six steps:
1. Requirements Gathering
• Similar to traditional requirements gathering, need to identify what the RESTful
services should do.
2. Resource Identification
• Similar to OOAD, need to identify list of objects to be used in services.
• These are typical model objects used within RESTful services.
3. Resource representation definition
• It is the representation of resources that are exchanged between clients and
servers, so we should define what kind of representation we need to use.
• Typically, we use XML, but JSON has gained popularity for its simplicity.
4. URI Definition
• Consists of URIs for clients and servers to exchange resources
representations
15
… REST – How to?
5. Implementation
• Environment Setup: Eclipse, Tomcat6, Jersey, Dynamic Web project
• Controller: Receives and processes incoming Restful requests. Use JAX-RS
Annotations for URL based method invocations
• EmployeeManager: Provides CRUD on Employees
• DepartmentManager: Provides CRUD on Departments
• SearchManager: Provides Search feature on employees and departments
• Model: Resources whose representations are exchanged between the client
and the server
• Employee, Department
• Business/Data Logic: Perform operations on underlying data.
• StorageService: All data operations are on done using static HashMaps for demo
purposes
16
… REST – How to?
6. Testing
Jersey Client API
• Originally used for Jersey Unit testing, but applicable as a general RESTful
client API
• Provides a high level wrapper over low level CRUD operations on resources
Java Http Client
• Jakarta Commons HttpClient provides an efficient and easy-to-use tool kit for
HTTP request/response operations
Test Harness
• Custom-built web application with a User interface to test RESTful Web
services
• Allows the user to select the “Accept” header, HTTP method, HTTP URL and
the Request payload
17
Q&A
Presentation and demo code can be downloaded from:
www.collabera.com/SIJC.jsp
Established in 1991, Collabera is a global, end-to-end information technology
consulting and services company. With over 5000 professionals operating from
19 locations and 3 development centers worldwide, Collabera provides onsite,
offsite and offshore services to global 2000 corporations.
For more information about the company, visit www.collabera.com
Thank You
Download