Introduction to Restful Web Services

54
Introduction to RESTful Web Services 09/2012 Wei Li

description

An introduction to restful web services

Transcript of Introduction to Restful Web Services

Page 1: Introduction to Restful Web Services

Introduction to RESTful Web Services

09/2012

Wei Li

Page 2: Introduction to Restful Web Services

Agenda

• RESTful in General

• URI Design

• Java Implementation

• Others

• Test

• Demonstration

Page 3: Introduction to Restful Web Services

RESTful in General

Page 4: Introduction to Restful Web Services

In a Nutshell

• RESTful is about resources

• RESTful is about how to represent the resource in different ways

• RESTful is about how to manipulate the resource

Page 5: Introduction to Restful Web Services

Define REST

• REST stands for Representational State Transfer

• An architecture style for designing networked applications

Page 6: Introduction to Restful Web Services

Define REST

• REST offers a simple, interoperable, and flexible way of writing web services that can be very different than the RPC mechanisms like CORBA and WS-*

Page 7: Introduction to Restful Web Services

RESTful Is Not

• A protocol

• A standard

• A replacement for SOAP

Page 8: Introduction to Restful Web Services

RESTful Architectural Principles

• Addressable resources

• A uniform, constrained interface

• Representation oriented

• Stateless communicate

• Hypermedia As The Engine Of Application State (HATEOAS)

Page 9: Introduction to Restful Web Services

Resources

• The key abstraction of information and data in REST

• Each resource must be addressable via a URI (Uniform Resource Identifier)

• Everything can be a resource

Page 10: Introduction to Restful Web Services

Addressability

• Every object and resource in the system should be reachable through a unique identifier

• Managed through the use of URIs

Page 11: Introduction to Restful Web Services

Uniform and Constrained Interface

• Don’t have an “action” parameter in URI

• Use only the methods of HTTP for the web services

• HTTP has a small fixed set of operational methods. Each method has a specific purpose and meaning

Page 12: Introduction to Restful Web Services

Representation Oriented

• The user interacts with services using representations of that service

• A resource referenced by one URI can have different formats. – HTML (for browsers)

– XML (for application)

– JSON (for JavaScript)

– Excel spreadsheet

– Image

Page 13: Introduction to Restful Web Services

Stateless Communication

• No client session data stored on the server

• If there are needs for session-specific data, it should be held and maintained by the client and transferred to the server with each request as needed

• A service layer that does not have to maintain client sessions is much easier to scale

Page 14: Introduction to Restful Web Services

RESTful and HTTP

• REST isn't protocol specific

• However when talking about REST, people usually mean REST over HTTP

• Benefits of using HTTP for RESTful services:

Familiarity

Interoperability

Interoperability

Page 15: Introduction to Restful Web Services

REST Triangle

Source: http://en.wikipedia.org/wiki/File:Resttriangle.svg

Page 16: Introduction to Restful Web Services

HTTP Methods

• GET

• PUT

• DELETE

• POST

• HEAD

• OPTIONS

Page 17: Introduction to Restful Web Services

CRUD Operation Mapped to HTTP Methods in RESTful

OPERATION HTTP METHOD

Create POST

Read GET

Update PUT

Delete DELETE

Page 18: Introduction to Restful Web Services

HTTP Response Code

• 200 OK

• 201 Created

• 202 Accepted

• 204 Success

• 301 Moved Permanently

• 302 Found

• 303 See Other

• 304 Not Modified

• 500 Internal Error

• 503 Service Unavailable

Page 19: Introduction to Restful Web Services

HTTP Response Code

• 401 Unauthorized

• 403 Forbidden

• 404 Not Found

• 405 Method Not Allowed

• 409 Conflict

• 411 Length Required

• 413 Entity Too Long

• 415 Unsupported Media Type

Page 24: Introduction to Restful Web Services

URI Design

Page 25: Introduction to Restful Web Services

URI

• Human meaningful

• Hierarchical

• Nouns

• No verbs

Page 26: Introduction to Restful Web Services

URI Examples

• http://localhost:9999/restapi/books – GET – get all books

– POST – add a new book

• http://localhost:9999/restapi/books/id – GET – get book whose id is provided

– PUT – update the book whose id is provided

– DELETE – delete the book whose is provided

Page 27: Introduction to Restful Web Services

URI Examples

• Twitter REST API v1.1 example: – https://dev.twitter.com/docs/api/1.1

Page 28: Introduction to Restful Web Services

Content Negotiation

Page 29: Introduction to Restful Web Services

Content Negotiation and URI

• http://localhost:9999/restapi/books/id.xml

• http://localhost:9999/restapi/books/id.json

• http://localhost:9999/restapi/books/id.pdf

Page 31: Introduction to Restful Web Services

Java Implementation

Page 32: Introduction to Restful Web Services

JSR-311

• POJO based

• HTTP centric

• Format independence

• Container independence

• Inclusion in Java EE

Page 33: Introduction to Restful Web Services

JSR-311 Annotations

Page 34: Introduction to Restful Web Services

JSR 311 Annotations

Page 35: Introduction to Restful Web Services

Available Java Frameworks

• Jersey

• CXF

• Wink

• RESTEasy

Page 36: Introduction to Restful Web Services

Others

Page 37: Introduction to Restful Web Services

WADL

• The Web Application Description Language

• A machine-readable XML description of HTTP-based web applications (typically RESTful web services).

Page 38: Introduction to Restful Web Services

WADL

• Example (using Jersey implementation)

http://localhost:9999/restapi/application.wadl

Page 41: Introduction to Restful Web Services

Security

• HTTPS (encrypt channel)

• Web ACL

• Others (more advanced)

Page 42: Introduction to Restful Web Services

Error Handling

• Use standard error message format as response

• Use Non-200 response codes

HTTP/1.1 400 Bad Request

Content-Type: application/xml;charset=UTF-8

Link: http://server/error/someexplanation

<error>

<message>You have made an invalid request...

</message>

</error>

Page 43: Introduction to Restful Web Services

Demonstration

• A simple one page web application http://mybooks.cloudfoundry.com/books.html

• Restful web service is implemented using Jersey

• Client consumes the service through JQUERY AJAX API and render the JSON data

Page 44: Introduction to Restful Web Services

Test

Page 45: Introduction to Restful Web Services

Test RESTful Web Services

• Using cURL

Page 46: Introduction to Restful Web Services

Test RESTful Web Services

• Using cURL

Page 47: Introduction to Restful Web Services

Test RESTful Web Services

• Using cURL

Page 48: Introduction to Restful Web Services

Test RESTful Web Services

• Using SOAPUI

Page 49: Introduction to Restful Web Services

Test RESTful Web Services

• Using SOAPUI

Page 50: Introduction to Restful Web Services

Test RESTful Web Services

• Using SOAPUI

Page 51: Introduction to Restful Web Services

Test RESTful Web Services

• Using SOAPUI

Page 52: Introduction to Restful Web Services

Test RESTful Web Services

• Using SOAPUI

Page 53: Introduction to Restful Web Services

Reference

• http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm