Download - Java Web Services [5/5]: REST and JAX-RS
![Page 1: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/1.jpg)
Assoc.Prof. Dr. Thanachart Numnondawww.imcinstitute.com
August 2010
Topic 5
REST and JAX-RS
![Page 2: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/2.jpg)
2
Agenda
What is REST?
REST/HTTP Methods
JAX-RS
![Page 3: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/3.jpg)
3
What is REST?
![Page 4: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/4.jpg)
4
REST : Definition [Wikipedia]
Representational State Transfer a style of software architecture for distributed
hypermedia systems such as the World Wide Web.
was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation.
Conforming to the REST constraints is referred to as being ‘RESTful’.
![Page 5: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/5.jpg)
5
REST is ...
An architectural style, not technology– Client/server + Request/response approach.
Everything is a RESOURCE. CRUD (Create / Read / Update / Delete) Stateless by nature (excellent for distributed
systems) Cacheable (naturally supported !) A great way to web-service !
![Page 6: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/6.jpg)
6
Architectural overview
![Page 7: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/7.jpg)
7
Architectural overview
![Page 8: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/8.jpg)
8
HTTP request overview
![Page 9: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/9.jpg)
9
HTTP request overview
![Page 10: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/10.jpg)
10
Everything is a resource …
A resource is …– A network-accessible data object or service
identified by an URI:– Images,– Documents (HTML, PDF, …),– Geo-location,– Weather
![Page 11: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/11.jpg)
11
Resource
Collections– http://portal/books/
Members/Items:– http://portal/documents/mybook.doc
![Page 12: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/12.jpg)
12
Characteristics of REST
RESTful services are stateless– Each request from client to server must contain all
the information necessary to understand the request
RESTful services have a uniform interface– GET, POST, PUT, and DELETE.
REST-based architectures are built from resources (pieces of information) that are uniquely identified by URIs
– In a RESTful purchasing system, each purchase order has a unique URI
![Page 13: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/13.jpg)
13
Characteristics of REST
REST components manipulate resources by exchanging representations of the resources
– For example, a purchase order resource can be represented by an XML document.
– Within a RESTful purchasing system, a purchase order might be updated by posting an XML document containing the changed purchase order to its URI
REST-based architectures communicate primarily through the transfer of representations of resources
– State is maintained within a resource representation
![Page 14: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/14.jpg)
14
REST/HTTP Methods
![Page 15: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/15.jpg)
15
CRUD to HTTP method mapping
![Page 16: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/16.jpg)
16
CRUD Operations are Performed through “HTTP
method”+“Resource”
CRUD Operations HTTP method Resource
Create (Single) POST Collection URI
Read (Multiple) GET Collection URI
Read (Single) GET Entry URI
Update (Single) PUT Entry URI
Delete (Single) DELETE Entry URI
![Page 17: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/17.jpg)
17
HTTP Methods
/books/– POST - submit a new book– GET - list all books
/books/{isbn}/– GET - get a book– PUT - update a book– DELETE - remove a book
![Page 18: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/18.jpg)
18
HTTP Methods: GET
GET to retrieve information Retrieves a given URI Idempotent, should not initiate a state change Cacheable
![Page 19: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/19.jpg)
19
HTTP Methods: POST
POST to add new information add the entity as a subordinate/append to the
POSTed resource Example
– POST /music/beatles
Adds the music specified in the POSTDATA to the
list of music
![Page 20: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/20.jpg)
20
HTTP Methods: DELETE
Remove (logical) an entity Example
– DELETE /songs/heyjude
Delete the song 'heyjude” from the system
![Page 21: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/21.jpg)
21
HTTP Methods: POST
POST to add new information add the entity as a subordinate/append to the
POSTed resource Example
– POST /music/beatles
Adds the music specified in the POSTDATA to the
list of music
![Page 22: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/22.jpg)
22
Why REST ?
• Uniformity:– URI is a uniform way to identify resources– HTTP uniform interface to manipulate resources
• REST base services are easy to work with– Do not need specialized API, just need to
understand HTTP and browser for experimentation
• Scripting language friendly– Easy to consume and develop
![Page 23: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/23.jpg)
23
Advantages of REST
• Its architectural constraints when applied as a whole, generate:
– Scalable component interactions– General interfaces– Independently deployed connectors– Reduced interaction latency– Strengthened security– Safe encapsulation of legacy systems
![Page 24: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/24.jpg)
24
REST vs “Big” Web Services
• “Big” web service– Few URIs (nouns), many custom methods
(verbs)• musicPort.getRecordings(“beatles”)
– Uses HTTP as transport for SOAP messages
• RESTful web service– Many resources (nouns), few fixed
methods(verbs)• GET /music/artists/beatles/recordings
– HTTP is the protocol
![Page 25: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/25.jpg)
25
JAX-RS
![Page 26: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/26.jpg)
26
JAX-RS (JSR-311) : Goals
• POJO-based,
• HTTP-centric,
• Format independent,
• Container independent,
• Availability as standalone and enterprise platforms.
![Page 27: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/27.jpg)
27
JAX-RS: Address-ability through URI
• A Web service exposes data as resources
• A resource is exposed through a URI
• Resources are “plain old” Java classes and methods
• The annotation @Path exposes a resource
• Think resources and URIs using Java classes and @Path
![Page 28: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/28.jpg)
28
POJO + Annotation = JAX-RS resource
![Page 29: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/29.jpg)
29
Reading the catalog
![Page 30: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/30.jpg)
30
Reading the catalog item
![Page 31: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/31.jpg)
31
Address-ability through HTTP Methods
• Methods: what are the HTTP methods?
• HTTP methods implemented as Java methods annotated with
– @POST– @GET – @PUT
– @DELETE
![Page 32: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/32.jpg)
32
Uniform interface: methods on root resources
@Path(“/books/”)class books { @POST <type> create(<type>) { ... } @GET <type> get() { ... }}
@Path(“/books/{isbn}/”)class book { @GET <type> get(...) { ... } @PUT void update(...) { ... } @DELETE void delete(...) { ... }}
Java method name is not significantThe HTTP method is the method
![Page 33: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/33.jpg)
33
Leading JAX-RS implementations
• Glassfish Jersey project
• RESTEasy(JBoss)
• Apache CXF (Apache Software Foundation)
• Wink (ASF incubation project)
• Restlet(NoeliosTechnologies)
![Page 34: Java Web Services [5/5]: REST and JAX-RS](https://reader034.vdocument.in/reader034/viewer/2022051610/549606f0ac7959342e8b4ffc/html5/thumbnails/34.jpg)
34
Resources
Some contents are borrowed from the presentation slides of Sang Shin, Java™ Technology Evangelist, Sun Microsystems, Inc.
JAX-RS… and the REST will follow, Guy Nir, Java Edge 09