Beruflich Dokumente
Kultur Dokumente
Chapitre 5
Sommaire
Qu'est ce que REST ?
Le format JSON
Mise en uvre de REST avec JAX-RS et Jersey
Mise en uvre de REST avec Apache-CXF
Mise en uvre de REST avec REST-Easy de JBoss
Mise en uvre de REST avec Spring MVC
REST et le Web
REST est le style architectural original du Web !
Tous les services Web que nous utilisons depuis des annes ,services
de commande des livres , services de recherche ,dictionnaire en ligne
sont des Web services bass sur REST.
Donc, vous tes entrain dutiliser REST sans le savoir !
Client
Fuel ncessaire
Maintenance prvue
.
Boeing747.html
Le client rfrencie une ressource Web grace une URL.Il reoit une
reprsentation de la ressource(ici un doc HTML).
La nouvelle reprsentation (Boeing747.html) place lapplication cliente
dans un autre tat(state).
Ainsi, lapplication cliente change(transfers) dtat avec chaque
reprsentation
Code la demande:
REST permet lextension des fonctionnalits dun client en tlchargeant et excutant
du code sous forme d'applet ou de scripts.
Amliore l'extensibilit du systme grce la possibilit de tlcharger des
fonctionnalits aprs le dploiement.
10
11
REST Web-services
HTTP methods
GET defines a reading access of the resource without side-effects. The
resource is never changed via a GET request, e.g. the request has no
side effects (idempotent).
PUT creates a new resource, must also be idempotent.
DELETE removes the resources. The operations are idempotent, they
can get repeated without leading to different results.
POST updates an existing resource or creates a new resource.
12
REST Web-services
Example
base URI for the web service: http://example.com/resources/
Resource
GET
PUT
POST
DELETE
Retrieve a
representation of the
addressed member of
the collection,
expressed in an
appropriate Internet
media type.
Replace the
addressed member
of the collection, or
if it doesn't
exist, create it.
Delete the
addressed
member of the
collection.
13
Le format JSON
JSON Notation
A lightweight data-interchange format
A subset of the object literal notation of JavaScript (or ECMA-262).
A JSON string must be enclosed by double quotes.
Comparison of JSON and XML
Similarities:
Both are human readable/Both have very simple syntax
Both are hierarchical/Both are language independent
Both can be used by Ajax
Differences:
Syntax is different
JSON is less verbose
JSON includes arrays
Names in JSON must not be JavaScript reserved words
XML can be validated
JavaScript is not typically used on the server side
15
JSON example
JSON stands for JavaScript Object Notation
Despite the name, JSON is a (mostly) language-independent way of specifying
objects as name-value pairs
Example (http://secretgeek.net/json_3mins.asp):
{"skillz": {
"web":[
{ "name": "html",
"years": 5
},
{ "name": "css",
"years": 3
}]
"database":[
{ "name": "sql",
"years": 7
}]
}}
16
JSON syntax, I
An object is an unordered set of name/value pairs
The pairs are enclosed within braces, { }
There is a colon between the name and the value
Pairs are separated by commas
Example: { "name": "html", "years": 5 }
An array is an ordered collection of values
The values are enclosed within brackets, [ ]
Values are separated by commas
Example: [ "html", xml", "css" ]
17
JSON syntax, II
A value can be: A string, a number, true, false, null, an object, or an
array
Values can be nested
Strings are enclosed in double quotes, and can contain the usual
assortment of escaped characters
Numbers have the usual C/C++/Java syntax, including exponential (E)
notation
All numbers are decimal--no octal or hexadecimal
Whitespace can be used between any pair of tokens
18
JavaScript object
JavaScript value
19
20
10
JAX-RS
JSR-311: JAX-RS: The Java API for RESTful Web Services
Introduced in Java SE 5: annotation-based
Objective: simplify the development and deployment of web
service clients and endpoints.
Part of Java EE 6
No configuration is necessary to start using JAX-RS
22
11
JAX-RS API
Specifications
Annotations to aid in mapping a resource class (a Plain Old
Java Object) as a web resource.
@Path specifies the relative path for a resource class or method.
@GET, @PUT, @POST, @DELETE and @HEAD specify the HTTP request type of a
resource.
@Produces specifies the response MIME media types.
@Consumes specifies the accepted request media types.
23
JAX-RS API
24
12
JAX-RS implementations
Implementations
Jersey, from Sun/Oracle (Java EE 6 reference implementation)
Apache, CFX from Apache
RESTeasy, from JBoss
Restlet, created by Jerome Louvel, a pioneer in REST frameworks.
25
Jersey
Jersey is the reference implementation for the JSR 311 specification.
The Jersey implementation provides a library to implement Restful
webservices in a Java servlet container.
On the server side Jersey provides a servlet implementation which
scans predefined classes to identify RESTful resources.
In your web.xml configuration file your register this servlet for your web
application.
The Jersey implementation also provides a client library to communicate
with a RESTful webservice.
JAX-RS supports the creation of XML and JSON via the Java
Architecture for XML Binding (JAXB).
26
13
27
14
Contd
29
30
15
Contd
M.Romdhani, INSAT, Dcembre 2015
32
16
33
17
36
18