810 likes | 1.74k Views
RESTful Web Services. by. 29–Oct–2010. 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
E N D
RESTful Web Services by 29–Oct–2010 Senthil Chinnaiyan, Senior Architect Chandra Ramachandran, Architect
Agenda • Part I • Introduction • What is REST? • How does it work? • RESTful Architecture • REST vsSOAP • REST – Why and When? • Part II • JAX-RS • Jersey • RESTful Service – Deep dive • Designing Service • Implementing Service • Q & A
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, there is tremendous interest in REpresentationalState Transfer (REST) architectural style
What is REST? • REST stands for “REpresentationalState Transfer”, 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.
How does REST work? … • Resources can be anything, simple entity or entities • Actual data of the system and can be in any form • Example: Customer, Orders etc • 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 REST
… 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 used for operation • URI is used to locate resources 2 3 1 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 data and to generate Representation in XML • Servlet container sends back the response as XML with 200 as the status code
RESTfulArchitecture • A typical Java based RESTful architecture consists of 3 layers. • The Restful 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 data logic and takes care of O/R mapping.
REST vsSOAP? • 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
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
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’sRESTEasy, RESTlet, Apache Wink
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
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
… 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
… 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
Q & APresentation 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