You are on page 1of 23

RESTStyleWeb Services

C3: Protected
About the Author

Created By: Pratik Das, 123011

Credential <Enter the technical qualification and experience of the author>


Information:

Version and V1.0 , 09-05-2010


Date:

© 2007, Cognizant Technology Solutions Confidential 2


Icons Used

Hands on
Questions Tools Exercise

Coding Test Your


Reference
Standards Understanding

A Welcome
Demonstration Contacts
Break

© 2007, Cognizant Technology Solutions Confidential 3


RESTful Web Services-Development: Overview

 Creating a RESTful web service


 Consuming a RESTful web service

© 2007, Cognizant Technology Solutions Confidential 4


RESTful Web Service development: Objectives

 Objective:
After completing this chapter you will be able to:
 Create a RESTful web service
 Consume a RESTful web service

© 2007, Cognizant Technology Solutions Confidential 5


Introduction
 Representational State Transfer (REST) is an architectural style
of networked systems created based on how the web works.
 In the REST architectural style, every piece of information is a resource,
and these resources are addressed using Uniform Resource Identifiers
(URIs), typically links on the Web.
 The resources are acted upon by using a set of simple, well-defined
operations.
 The REST client-server architectural style is designed to use a stateless
communication protocol, typically HTTP.
 In REST, clients and servers exchange representations of resources using a
defined interface and protocol. These principles encourage RESTful
applications to be simple and lightweight, and have high performance

© 2007, Cognizant Technology Solutions Confidential 6


REST Principles…

Addressability
 Addressability is the idea that every object and resource in
your system is reachable through a unique identifier.
 In the REST world, addressability is managed through the use
of URIs.
 The format of a URI is standardized as follows:
scheme://host:port/path?queryString#fragment
The Uniform, Constrained Interface
 The idea behind it is that you stick to the finite set of
operations of the application protocol you’re distributing your
services upon.

© 2007, Cognizant Technology Solutions Confidential 7


…REST Principles

Representation-Oriented
 In a RESTful system, the complexity of the client-server
interaction is within the representations being passed back
and forth. These representations could be XML, JSON etc
Communicate Statelessly
 Stateless means that there is no client session data stored on
the server. The server only records and manages the state of
the resources it exposes.
HATEOAS
 The final principle of REST is the idea of using Hypermedia As
The Engine Of Application State (HATEOAS).

© 2007, Cognizant Technology Solutions Confidential 8


Components of RESTful Web Service

Representatio
Resources URI
ns
• Anything the • A unique • Any useful
client might identifier for information
want to a resource, about the
reference or made of a state of a
interact with, name and a resource
any piece of structured
information address
that might be indicating
worthwhile where to find
referencing this resource.
in a
hyperlink

© 2007, Cognizant Technology Solutions Confidential 9


RESTful Web Service standards

 REST applications use the following standards:


 HTTP
 URI
 XML, JSON, HTML, GIF, JPEG, etc
 JAX-RS is the specification used in java.

 Available JAX-RS implementations:


 Jersey
 CXF
 RESTEasy
 Restlet

© 2007, Cognizant Technology Solutions Confidential 10


When to use REST

 The web services are completely stateless.


 The service producer and service consumer have a mutual understanding
of the context and content being passed along.
 REST is particularly useful for limited-profile devices such as PDAs and
mobile phones, for which the overhead of headers and additional layers of
SOAP elements on the XML payload must be restricted.
 Web service delivery or aggregation into existing web sites can be enabled
easily with a RESTful style using technologies such as AJAX to consume the
services in their web applications
SOAP based web services are used in following situations:
 A formal contract must be established to describe the interface that the
web service offers (using WSDL).
 The architecture must address complex nonfunctional requirements.
 The service needs to handle asynchronous processing and invocation

© 2007, Cognizant Technology Solutions Confidential 11


REST, CRUD and HTTP verbs
 Services often exchange uniquely identified resources, linked with others like
hyperlinks.
 To create, update, and delete a book resource, use the common HTTP verbs- for
example:
 Use POST on data (in XML, JSON, or text format) to create the book resource
with the URI http://url/books/. Once created, the response sends back the URI
of the new resource: http://url/books/123456.
 Use GET to read the resource (and possible links to other resources from the
entity body) at http://url/books/123456.
 Use PUT on data to update the book resource at http://url/books/123456.
 Use DELETE to delete the resource at http://url/books/123456.
 By using HTTP verbs, we have access to all possible Create, Read, Update, Delete
(CRUD) actions on a resource.

© 2007, Cognizant Technology Solutions Confidential 12


Writing a REST service

 Resources are POJOs that have at least one method annotated


with @javax.ws.rs.Path.

 The BookResource is a Java class annotated with @Path, indicating that the resource
will be hosted at the URI path /book.
 The getBookTitle() method is marked to process HTTP GET requests (using @GET
annotation) and produces text (the content is identified by the MIME Media
text/plain).
 To access this resource, you need an HTTP client such as a browser and an HTTP
GET method to point to the URL http://localhost:8080/bookweb/book.

© 2007, Cognizant Technology Solutions Confidential 13


More about REST
 JAX-RS is HTTP-centric by nature and has a set of clearly defined classes and
annotations to deal with HTTP and URIs.
 A resource can have several representations, so the API provides support for a
variety of content types and uses JAXB to marshall and unmarshall XML and JSON
representations into objects.
 JAX-RS is also independent of the container, so resources can be deployed in a
variety of servlet containers.
 Requirements for REST service
 The class must be annotated with @javax.ws.rs.Path.
 To add EJB capabilities to a REST service, the class has to be annotated with
@javax.ejb.Stateless.
 The class must be defined as public, and it must not be final or abstract.
 Root resource classes (classes with a @Path annotation) must have a default
public constructor. Nonroot resource classes do not require such a constructor.
 The class must not define the finalize() method.

© 2007, Cognizant Technology Solutions Confidential 14


URI definition

 The @Path annotation's value is a relative URI path. When used on classes,
these are referred to as root resources, providing the roots of the resource
tree and access to subresources.
 URI path templates are also used with variables embedded within the URI
syntax. Those variables are evaluated at runtime.
@Path("/customers/{customername}")
 @Path may also be used on methods of root resources, which can be useful
to group together common functionalities for several resources
 If @Path exists on both the class and method, the relative path to the
resource method is a concatenation of the class and method. For example,
to get a book by its ID, the path will be /items/books/1234

© 2007, Cognizant Technology Solutions Confidential 15


Extracting parameters

 @PathParam
 @Path("/books/{bookid}")
 public Book getBook(@PathParam("bookid") String bookId)
 Uri: http://url/customers/98342

 @QueryParam
 public Customer getCustomerByZipCode(@QueryParam("zip") Long zip) {

 Uri: http://url/customers?zip=19870

© 2007, Cognizant Technology Solutions Confidential 16


Producing and consuming content types

 With REST, a resource can have several representations; a


book can be represented as a web page, XML data, or an image
showing the album cover.
 The @javax.ws.rs.Consumes and @javax.ws.rs.Produces
annotations may be applied to a resource where several
representations are possible.
 It defines the media types of the representation exchanged
between the client and the server.
 Use of these annotations on a resource method overrides any
on the resource class for a method argument or return type.
 In the absence of either of these annotations, support for any
media type (*/*) is assumed.
© 2007, Cognizant Technology Solutions Confidential 17
Content Type

 If a resource is capable of producing more than one Internet


media type, the resource method chosen will correspond to
the most acceptable media type, as declared by the client in
the Accept header of the HTTP request

 The getBookAsJson() method returns a representation of type


application/json. JSON is a lightweight data-interchange
format that is less verbose and more readable than XML.
© 2007, Cognizant Technology Solutions Confidential 18
Consuming REST services

 From Java code


 From javascript in browser using AJAX technology
 Examples:
var xhr = new XMLHttpRequest();
var ajaxurl="http://localhost:8086/TrainRestWS/rest/books/booktitle/"+bookId;
xhr.open("GET", ajaxurl, true);
xhr.send(null)

© 2007, Cognizant Technology Solutions Confidential 19


RESTStyle Web Services

Thank You !

© 2007, Cognizant Technology Solutions Confidential 20


RESTStyle Web Services

 Questions from participants

© 2007, Cognizant Technology Solutions Confidential 21


RESTStyle Web Services : Source

 http://www.eclipse.org/webtools
 http://www.w3.org/TR/wsdl
 http://ws.apache.org/axis2/

Disclaimer: Parts of the content of this course is based on the materials available from the Web sites and books
listed above. The materials that can be accessed from linked sites are not maintained by Cognizant Academy and
we are not responsible for the contents thereof. All trademarks, service marks, and trade names in this course are
the marks of the respective owner(s).

© 2007, Cognizant Technology Solutions Confidential 22


You have successfully completed
RESTStyle Web Services

You might also like