RESTful Web Services

abnormalobeisanceΑσφάλεια

3 Νοε 2013 (πριν από 4 χρόνια και 7 μέρες)

78 εμφανίσεις


RESTful Web Services

29

Oct

2010

by

Senthil Chinnaiyan
,
Senior Architect

Chandra Ramachandran
,
Architect

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

3

Part I


REST Concepts

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,
t
here
is tremendous interest in
RE
presentational

S
tate
T
ransfer
(REST)
architectural
style




4

What is REST?



REST stands for “
RE
presentational

S
tate
T
ransfer”, 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.




5

Browser
REST Server
(
Web Services
)
REST Server
(
Web Services
)
Application
Application
HTTP Request
HTTP Response
XML
,
JSON
,
HTML
,
TEXT etc
HTTP Request
HTTP Response
XML
,
JSON
,
HTML
,
TEXT etc
Resources
Representation
URI
Text
How does REST work? …



Hyperlink
to
a resource and the
only way of exchange data
between client and server



Each resource can be accessed
by unique ID called URI



Example
:

<domain>/
depts
/

<domain>/
depts
/finance

<domain>/employees/
senthil




Representation is a temporal
state of the actual
data



Representation is sent
back and
forth between client and
server



Example:
XML, JSON, Text



Resources can be
anything,
simple entity or entities



Actual
data of the system and
can be in any
form



Example:

Customer, Orders
etc

… 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.






Accept header is

used for representation




HTTP method is

u
sed for operation




URI is used to

l
ocate resources


7

1

2

3

RETRIEVE
UPDATE
INSERT
DELETE
PUT
GET
GET
/
emp
/
Senthil
HTTP
/
1
.
1
Accept
:
text
/
xml
...
DELETE
POST
POST
/
emp
/
Senthil

HTTP
/
1
.
1
Accept
:
text
/
xml
Content
-
Type
:
text
/
xml
<
xml
>

<
/
xml
>
PUT
/
emp
/
Senthil

HTTP
/
1
.
1
Accept
:
text
/
xml
Content
-
Type
:
text
/
xml
<
xml
>

<
/
xml
>
DELETE
/
emp
/
Senthil

HTTP
/
1
.
1
Accept
:
text
/
xml
Content
-
Type
:
text
/
xml
<
xml
>

<
/
xml
>
1

2

3

… 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



The
representation type
is set

through
the
Accept request header



REST Framework invokes

domain code to
retrieve

d
ata and to generate

Representation in XML



Servlet container sends back the

r
esponse as XML with 200 as

the status code



8

Servlet Container
REST Framework
(
REST Constraints
)
Domain Code
(
Business Logic
)
Client Application
GET
/
employees
/
Senthil
HTTP
/
1
.
1
Host
:
localhost
Date
:
Sat
,
23
Oct
2010
Accept
:
text
/
xml
...
HTTP
/
1
.
1 200
OK
Date
:
Sat
,
23
Oct
2010
Server
:
Apache
/
1
.
3
.
3
.
7
Content
-
Length
:
438
Connection
:
close
Content
-
Type
:
text
/
xml
;
charset
=
UTF
-
8
<
xml
>
<
emp
>
<
fname
>
Senthil
<
/
fname
>
<
lname
>
Chinnaiyan
<
/
lname
>
<
/
emp
>
<
/
xml
>
Response
Request
http
://
localhost
:
8080
/
restService
/
api
/
employees
/
senthil
RESTful Service
RESTful

Architecture



A typical Java based RESTful architecture consists of 3 layers.



The
R
estful 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.



The
Business Layer

uses appropriate

framework to execute the business logic.



The
Data Layer

is responsible for

d
ata logic and takes care of O/R mapping.






9

RESTful Service Layer
(
JAX
-
RS
)
Business Layer
(
Spring
/
Seam
/
EJB
...)
Data Layer
(
Hibernate
/
iBatis
/
EJB
...)
RESTful Architecture
Database
Client
Application
http
jdbc
REST
v
s

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

12

Part II


REST Deep Dive

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.











Some of the Implementations of JAX
-
RS:


Apache CXF, Sun’s Jersey,
Jboss’s

RESTEasy
,
RESTlet
, Apache Wink


13

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

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