1. Trang chủ
  2. » Giáo án - Bài giảng

Bài giảng Lập trình mạng RESTful Web Service - GV. Nguyễn Xuân Vinh

34 1,2K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 34
Dung lượng 841 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

RESTful Web ServiceA complex system that works is invariably found to have evolved from a simple system that worked Editor: Nguyễn Xuân Vinh... • A complex system that works is invariabl

Trang 1

RESTful Web Service

A complex system that works is invariably found to have evolved from a simple system that worked

Editor: Nguyễn Xuân Vinh

Trang 2

• A complex system that works is invariably

found to have evolved from a simple system that worked

—John Gall

Systemantics

Trang 6

Accept: image/gif, image/x-xbitmap,

image/jpeg, image/pjpeg, image/png, */

Trang 7

Date: Mon, 23 May 2005 22:38:34 GMT

Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)

Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT

Trang 8

User-Agent: XMLRPC::Client (Ruby 1.8.4)

Content-Type: text/xml; charset=utf-8

Trang 12

Representational State Transfer (REST)

systems.

design model

Group (TAG) in parallel with HTTP/1.1, based

on the existing design of HTTP/1.0

Trang 13

• Simple (conceptually and programmatically)

• Simpler and cleaner than SOAP

Trang 15

• A style of software architecture

• A Virtual state-machine

A network of web pages (a virtual state-machine),

where the user progresses through an application by selecting links (state transitions), resulting in the next page (representing the next state of the application) being

transferred to the user and rendered for their use.

Trang 16

REST key goals

enforce security and encapsulate legacy

systems

Trang 17

• Session state on the client

• Visibility, reliability and scalability

• Trade off (network performance, etc.)

• Cacheable

• A response can be cacheable

• Efficiency but reduce reliability

• Layered system

• System scalability

• Code on demand (optional)

• Extension after deployment

• Uniform Interface

• Simple

Trang 18

REST Data Elements

• Resources and Resource Identifiers

• Uniform Interface (GET, PUT, POST, DELETE)

• Resource Oriented

• Simple and simple is beautiful

-GET RETRIEVE Retrieve Safe,Idempotent,Cacheable

Representations

• HTML / XML / images / sounds / …

Trang 19

 Clients initiate requests to servers

 servers process requests and return appropriate responses

 Requests and responses are built around the transfer of representations of resources

 A resource can be essentially any coherent and

meaningful concept that may be addressed.

 A representation of a resource is typically a document

that captures the current or intended state of a resource

 Client begins sending requests when it is ready to make the transition to a new state (While one or more

requests are outstanding, the client is considered to be

in transition)

 The representation of each application state contains

links that may be used the next time the client chooses

to initiate a new state-transition

Trang 20

RESTful web APIs (RESTful web service)

A collection of resources, with four defined

aspects:

The base URI for the web API

http://example.com/resources/

The Internet Media Type

Set of operations (GET, PUT, POST, DELETE)

Trang 22

• Simple Object Access Protocol

• RPC protocol that go through firewalls

• Communication protocol between applications

• A format for sending messages

Trang 23

• User-driven interactions via forms

• Few operations (generic interface) on many resources

• URI: Consistent naming mechanism for resources

• Focus on scalability and performance of large scale distributed

hypermedia systems

SOAP

•“The Web is the universal transport for messages”

• Activity/Service oriented

• Orchestrated reliable event flows

• Many operations (service interface) on few resources

• Lack of standard naming mechanism

• Focus on design of integrated (distributed) applications

Trang 31

* A command-line application that fetches bookmarks from del.icio.us

* and prints them to strandard output.

*/

public class DeliciousApp

{

public static void main(String[] args)

throws HttpException, IOException, ParserConfigurationException,

SAXException, XPathExpressionException

{

if (args.length != 2)

{

System.out.println("Usage: java -classpath [CLASSPATH] "

+ "DeliciousApp [USERNAME] [PASSWORD]");

System.out.println("[CLASSPATH] - Must contain commons-codec, " +

"commons-logging, and commons-httpclient");

System.out.println("[USERNAME] - Your del.icio.us username");

System.out.println("[PASSWORD] - Your del.icio.us password");

System.out.println();

System.exit(-1);

}

Trang 32

// Set the authentication credentials.

Credentials creds = new UsernamePasswordCredentials(args[0], args[1]);

HttpClient client = new HttpClient();

client.getState().setCredentials(AuthScope.ANY, creds);

// Make the HTTP request

String url = "https://api.del.icio.us/v1/posts/recent";

GetMethod method = new GetMethod(url);

Trang 33

XPath xpath = XPathFactory.newInstance().newXPath();

NodeList bookmarks = (NodeList)xpath.evaluate("/posts/post",

doc,

XPathConstants.NODESET);

// Iterate over the bookmarks and print out each one

for (int i = 0; i < bookmarks.getLength(); i++)

{

NamedNodeMap bookmark = bookmarks.item(i).getAttributes();

String description = bookmark.getNamedItem("description")

Trang 34

/** Searching the web with Yahoo!'s web service using XML.*/

public class YahooSearch {

static final String BASE_URI =

// Fetch a resource: an XML document full of search results

String term = Reference.encode(args[0]);

String uri = BASE_URI + "?appid=restbook&query=" + term;

Response response = new Client(Protocol.HTTP).get(uri);

DomRepresentation document = response.getEntityAsDom();

// Use XPath to find the interesting parts of the data structure

String expr = "/ResultSet/Result/Title";

for (Node node : document.getNodes(expr)) {

System.out.println(node.getTextContent()); } } } }

Ngày đăng: 28/05/2015, 07:53

TỪ KHÓA LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm