2Agenda Part I Part II Q & A Introduction What is REST? How does it work?RESTful ArchitectureREST vs SOAPREST – Why and When?Part IIJAX-RSJerseyRESTful Service – Deep diveDesigning ServiceImplementing ServiceQ & A
4IntroductionIn this modern world, enterprises are in the need of implementing Service Oriented Architecture (SOA) to achieve maximum business benefitsSOAP/WSDL/UDDI - SOA stack is the most common way and have failed to live up to their promise in some casesDue 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
5What is REST?REST stands for “REpresentational State Transfer”, was introduced and defined in 2000 by the doctoral dissertation of Roy FieldingREST 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.The most important concept of REST is resources, which are identified by global IDs typically using URIs.URIs are used to connect clients and servers to exchange resources in the form of representations, various representations could be XML, JSON, HTML, TEXT etc.
6REST How does REST work? … Resources can be anything, simple entity or entitiesActual data of the system and can be in any formExample: Customer, Orders etcHyperlink to a resource and the only way of exchange data between client and serverEach resource can be accessed by unique ID called URIExample:<domain>/depts/<domain>/depts/finance<domain>/employees/senthilRepresentation is a temporal state of the actual dataRepresentation is sent back and forth between client and serverExample: XML, JSON, TextREST
7… How does it work? REST in action In REST URIs are used to connect clients and servers to exchange resources in the form of representationsIn order to exchange data, REST relies on basic HTTP protocol methods: GET, POST, PUT and DELETE.Accept header isused for representationHTTP method isused for operationURI is used tolocate resources231123
8… 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 identifierThe representation type is setthrough the Accept request headerREST Framework invokesdomain code to retrievedata and to generateRepresentation in XMLServlet container sends back theresponse as XML with 200 asthe status code
9RESTful ArchitectureA typical Java based RESTful architecture consists of 3 layers.The Restful Service layer intercepts incoming http requests to identify the representation, resource and the operationJAX-RS is thespecification defined bySun to create and useRESTful services inJava applications.The Business Layer uses appropriateframework to execute the business logic.The Data Layer is responsible fordata logic and takes care of O/R mapping.
10REST 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
11Why and When? Consider REST: REST may be considered if the service model is light, stateless, low cost, which requires improved data security and high scalabilityConsider 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 RESTREST operates on stateless model, which may not be friendly for stateful service model
13JAX-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 WinkAnnotationsDescription@PathRelative path for a resource classHTTP request type of a method@ProducesReturned MIME media types@ConsumesAcceptable request media types@Param**Parameter values
14JerseyJersey 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 BindingIntegrates well with Spring frameworkSupports WADL (Web Application Description Language). WADL is the REST equivalent for WSDL
15REST – How to? …RESTful web service life cycle can be summarized in the following six steps:1. Requirements GatheringSimilar to traditional requirements gathering, need to identify what the RESTful services should do.2. Resource IdentificationSimilar 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 definitionIt 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 DefinitionConsists of URIs for clients and servers to exchange resources representations
16… REST – How to? 5. Implementation Environment Setup: Eclipse, Tomcat6, Jersey, Dynamic Web projectController: Receives and processes incoming Restful requests. Use JAX-RS Annotations for URL based method invocationsEmployeeManager: Provides CRUD on EmployeesDepartmentManager: Provides CRUD on DepartmentsSearchManager: Provides Search feature on employees and departmentsModel: Resources whose representations are exchanged between the client and the serverEmployee, DepartmentBusiness/Data Logic: Perform operations on underlying data.StorageService: All data operations are on done using static HashMaps for demo purposes
17… REST – How to? 6. Testing Jersey Client API Originally used for Jersey Unit testing, but applicable as a general RESTful client APIProvides a high level wrapper over low level CRUD operations on resourcesJava Http ClientJakarta Commons HttpClient provides an efficient and easy-to-use tool kit for HTTP request/response operationsTest HarnessCustom-built web application with a User interface to test RESTful Web servicesAllows the user to select the “Accept” header, HTTP method, HTTP URL and the Request payload
18For more information about the company, visit www.collabera.com Q & A Presentation and demo code can be downloaded from: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