54
AASE – REST Web Service Antonius Rachmat C

AASE –REST Web Servicelecturer.ukdw.ac.id/anton/download/enterprise13.pdf– – tah un}/{bulan}/{tanggal} • Transfer XML, JSON, or both REST Way The REST way of Implementing the

Embed Size (px)

Citation preview

AASE – REST Web

ServiceServiceAntonius Rachmat C

What is REST?

• REST stands for Representation State Transfer

• REST is about accessing resources through a

universal interface

• Conceived in 2000 as a doctoral dissertation by Roy • Conceived in 2000 as a doctoral dissertation by Roy

Fielding (a principal author of HTTP 1.0 dan 1.1

protocol)

• RESTful URLs are addressable

– Sample: http://www.mysite.com/Products/Trek8300

• Links to related data

Why is it called

"Representation State Transfer"?

ResourceClienthttp://www.boeing.com/aircraft/747

Fuel requirements

Boeing747.html

Fuel requirements

Maintenance schedule

...

Roy Felding

Motivation for REST

• The Client references a Web resource using a URL.

• A representation of the resource is returned (in this case as an HTML document).

• The representation (e.g., Boeing747.html) places the client application in a state.

• The result of the client traversing a hyperlink in • The result of the client traversing a hyperlink in Boeing747.html is another resource is accessed.

• The new representation places the client application into yet another state.

• Thus, the client application changes (transfers) state with each resource representation --> Representation State Transfer!

Motivation for REST

• The motivation for developing REST was to create anarchitectural model for how the Web should work, such that it could serve as the guiding framework for the Web protocol standards.

• REST has been applied to describe the desired Web architecture, help identify existing problems, compare alternative solutions, and ensure that protocol extensions would not violate the core constraints that make the Web successful.

REST - not a Standard

• REST is not a standard.

• Why? Because REST is just an architectural style.

• You can only understand it, and design your Web

services in that style.

• While REST is not a standard, it does prescribe the

use of standards:– HTTP

– URL

– XML/HTML/GIF/JPEG/etc (Resource Representations)

– text/xml, text/html, image/gif, image/jpeg, etc (Resource Types,

MIME Types)

Characteristics of a REST-based

Network• Client-Server

• Stateless: each request from client to server must contain all the

information necessary to understand the request, and cannot take

advantage of any stored context on the server.

• Cache: to improve network efficiency responses must be capable of being

labeled as cacheable or non-cacheable.labeled as cacheable or non-cacheable.

• Uniform interface: all resources are accessed with a generic interface

(e.g., HTTP GET, POST, PUT, DELETE).

• Named resources - the system is comprised of resources which are named

using a URI.

• Interconnected resource representations - the representations of the

resources are interconnected using URLs, thereby enabling a client to

progress from one state to another.

RESTful Web Services

Resources

Res 1

Res 2

HTTP Request

URL

GET Res 2

Res 3

Res 4

HTTP Response

Status

GET

POST

PUT

DELETEXML JSON

Payload

XML JSON

321root

Example – Resource Manager

/

Sessions/

Abcde/

Efgh/

Resources/ Dispatchers/ Xyz/

Universal Interface: HTTP verbs

• GET– Retrieves a resource

– Cacheable

• POST– Creates a new resource– Creates a new resource

• PUT– Updates an existing resource

• DELETE– Remove a resource

HTTP Method

Why REST?

• Nice URLs that can be guessed

• SEO : Search Engine Optimization

• Consistency with the design of the World Wide WebWide Web

• Can leverage generic client components (eg. jQuery controls)

Why not REST?

• If you need advanced collaboration between

applications like distributed transactions (WS-

*)

– WS-Security, WSDL, SOAP, etc– WS-Security, WSDL, SOAP, etc

• If you need to use a protocol other than HTTP

HTTP Method

• Misalkan ada “Resource” dengan URI berikut :

– http://www.ukdw.ac.id/mahasiswa/

• Resource (data) mahasiswa pada domain

ukdw.ac.id, diakses menggunakan protokol ukdw.ac.id, diakses menggunakan protokol

HTTP

• Untuk mendapatkan data mahasiswa dengan

NIM 22091234

– GET

http://www.ukdw.ac.id/mahasiswa/22091234/

Representasi Resource

• GET http://www.ukdw.ac.id/mahasiswa/22091234

• Dalam format XML :

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><?xml version="1.0" encoding="UTF-8" standalone="yes" ?><mahasiswa>

<nama>Udin</nama><tahunmasuk>2009</tahunmasuk>…

</mahasiswa>

REST Design Principle

• Use HTTP Method explicitly

– GET, POST, DELETE, and PUT

• Be Stateless

– HTTP is stateless– HTTP is stateless

• Expose directory structure-like URI

– http://www.berita.com/olahraga/sepakbola

– http://www.berita.com/olahraga/bulutangkis/{tahun}/{bulan}/{tanggal}

• Transfer XML, JSON, or both

REST Way

The REST way of Implementing

the Web Services

Response

(HTML/XML doc)

HTTP GET request URL 1

HTTP response

Parts

List

We

b S

erv

er

Response

(HTML/XML doc)

HTTP GET request URL 2

HTTP response

HTTP POST URL 3

HTTP responseURL to submitted PO

PO

(HTML/XML)

PartNo

PO

The REST way of Implementing

the Web Service

• Service: Get a list of parts

– The web service makes available a URL to a parts list

resource.

• http://www.parts-depot.com/parts• http://www.parts-depot.com/parts

• Note that how the web service generates the parts list is

completely transparent to the client.

– The web service may wish to allow the client to specify

whether he/she wants the parts list as an HTML

document, or as an XML document.

• http://www.parts-depot.com/parts?flavor=xml

Data Returned - Parts List

<?xml version="1.0"?>

<p:Parts xmlns:p="http://www.parts-depot.com"

xmlns:xlink="http://www.w3.org/1999/xlink"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation=

"http://www.parts-depot.com

http://www.parts-depot.com/parts.xsd">

<Part id="00345" xlink:href="http://www.parts-depot.com/parts/00345"/><Part id="00345" xlink:href="http://www.parts-depot.com/parts/00345"/>

<Part id="00346" xlink:href="http://www.parts-depot.com/parts/00346"/>

<Part id="00347" xlink:href="http://www.parts-depot.com/parts/00347"/>

<Part id="00348" xlink:href="http://www.parts-depot.com/parts/00348"/>

</p:Parts>

•Note that the parts list has links to get detailed info about each part.

The REST way of Implementing

the Web Service

• Service: Get detailed information

about a particular part

– The web service makes available a URL to each – The web service makes available a URL to each

part resource. Example, here's how a client

requests a specific part:• http://www.parts-depot.com/parts/00345?flavor=xml

Data Returned - Part

<?xml version="1.0"?>

<p:Part xmlns:p="http://www.parts-depot.com"

xmlns:xlink="http://www.w3.org/1999/xlink"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation=

"http://www.parts-depot.com

http://www.parts-depot.com/part.xsd">

<Part-ID>00345</Part-ID><Part-ID>00345</Part-ID>

<Name>Widget-A</Name>

<Description>This part is used within the frap assembly</Description>

<Specification xlink:href="http://www.parts-

depot.com/parts/00345/specification"/>

<UnitCost currency="USD">0.10</UnitCost>

<Quantity>10</Quantity>

</p:Part>

Each response document allows the client to drill down to get more detailed

information.

SOAP Way

Implementing the Web Services

using SOAP

Request

(XML doc)

Response

(XML doc)

SOAP envelope

HTTP POSTURL 1

HTTP Response

getPartsList()

Request

(XML doc)HTTP POST

URL 1

We

b S

erv

er(XML doc)

Response

(XML doc)

HTTP POSTURL 1

HTTP ResponsegetPart(id)SOAP Server

The SOAP Server parses the SOAP message to determine which method to invoke. All

SOAP messages are sent using an HTTP POST.

PO

(XML doc)HTTP POST

URL 1

submit(PO)

Response

(XML doc)HTTP Response

Implementing the Web Service

using SOAP• Service: Get a list of parts

– The client creates a SOAP document that specifies the

procedure desired.

<?xml version="1.0"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<p:getPartsList xmlns:p="http://www.parts-depot.com"/>

</soap:Body>

</soap:Envelope>

Data Returned - Parts List<?xml version="1.0"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<p:getPartsListResponse xmlns:p="http://www.parts-depot.com">

<Parts>

<Part-ID>00345<Part-ID>

<Part-ID>00346<Part-ID>

<Part-ID>00347<Part-ID><Part-ID>00347<Part-ID>

<Part-ID>00348<Part-ID>

</Parts>

<p:getPartsListResponse>

</soap:Body>

</soap:Envelope>

Note the absence of links. A URL that points to a SOAP

service is meaningless since the URL to a SOAP service is just to the

SOAP server

Implementing the Web Service

using SOAP• Service: Get detailed information about a particular part

– The client creates a SOAP document that specifies the procedure

desired, along with the part-id parameter.

<?xml version="1.0"?><?xml version="1.0"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<p:getPart xmlns:p="http://www.parts-depot.com">

<part-id>00345</part-id>

</p:getPart>

</soap:Body>

</soap:Envelope>

Data Returned - Part

<?xml version="1.0"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<p:getPartResponse xmlns:p="http://www.parts-depot.com">

<Part-ID>00345</Part-ID>

<Name>Widget-A</Name><Name>Widget-A</Name>

<Description>This part is used within the frap assembly</Description>

<UnitCost currency="USD">0.10</UnitCost>

<Quantity>10</Quantity>

</p:getPartResponse>

</soap:Body>

</soap:Envelope>

notice the absence of links. Thus, there is nothing in the response

to enable a client to "go to the next level of detail".

REST flow

SOAP flow

Key Principles of REST• "Identification of resources"

– Resources are anything that can be named

– Naming authority assigns an identifier to a resource

• "Manipulation of resources through representations“

– Representations are transferred between components

– Representations often contain links to related resources– Representations often contain links to related resources

• "Self-descriptive messages"

– Resource identifiers, representation data formats, control data

• "Hypermedia as the engine of application state"

– Servers are stateless, messages are independent

– Clients maintain state (representations) & traverse links

SOAP from a REST viewpoint:

Addressing

• REST architectures utilize the existing web addressing model

– Standardized URI schemes subsume protocols (http, ftp, etc.)

– Standardized distributed naming authorities (DNS)– Standardized distributed naming authorities (DNS)

– Standardized way of discovering, referring to resources (URIs)

• SOAP applications define their own addressing schemes

– Resources have custom, service-specific addresses

– No standardized way of discovering, referring to resources

SOAP from a REST viewpoint: Generic

Interfaces

• REST emphasizes standardized, generic operations

– Technique widely used (SQL, file system, registry,

Hailstorm)

– HTTP provides CRUD-like PUT, GET, POST, DELETE

– Allows for uniform manipulation of URI-identified – Allows for uniform manipulation of URI-identified

resources

• SOAP does not provide for generic operations but

standard

– Each application defines it's own set of operations

SOAP from a REST viewpoint: State

Management

• REST applications have explicit state transitions

– Servers & intermediaries are inherently stateless

– Resources contain data, links to valid state transitions

– Clients maintain state, traverse links in generic manner

• SOAP applications have implicit state transitions• SOAP applications have implicit state transitions

– Servers & intermediaries may (should!) be stateless

– Messages contain only data (not valid state transitions)

– Clients maintain state, require knowledge of state

machine

REST from a SOAP viewpoint:

Counterpoint

• SOAP & related technologies have broad industry

support

• SOAP client & server toolkits are widely deployed

• WS specifications provide end-to-end • WS specifications provide end-to-end

protocols/features

– HTTP/REST only provides point-to-point solutions

• SOAP can be bound to other, non-HTTP transports

– Important for richer XML messaging in the future

Demo REST dgn VB.NET

• Buat REST project dengan cara:

• Pada VS 2010

– New WCF Service Application

• Hapus file IService1.vb dan Service1.svc• Hapus file IService1.vb dan Service1.svc

• Add - new item: WCF Service

– Misal beri nama RestService.svc

• Tambahkan method pada interface dan URL Stylenya

• Implementasikan method tersebut

• Edit file web.config

Buat service pada IRestService.vb

Yang perlu diperhatikan WebGet

• UriTemplate: digunakan untuk mendefinisikan

bentuk format URL yang akan digunakan

untuk pengaksesan resource pada REST

• BodyStlye: digunakan untuk mengeset bentuk • BodyStlye: digunakan untuk mengeset bentuk

pengiriman data (bare, wrapped, dll)

• RequestFormat dan ResponseFormat: untuk

mengatur bagaimana cara menampilkan data

request dan response

– Bisa dalam format XML atau JSON

Implementasikan Interface tadi

Lanjutan (2)

Buat class Mahasiswa

Setting default pada web.config

Yang penting pada web.config

• <behavior name=“[namabehavior]”>

• Harus ada <endpointBehaviors>

<endpointBehaviors>

<behavior name=“[namaendpoint]"><behavior name=“[namaendpoint]">

<webHttp/>

</behavior>

</endpointBehaviors>

• Harus ada <services>

Yang penting pada web.config

<services>

<service behaviorConfiguration=“[namabehavior]" name=“[namanamespace.namaclass]">

<endpoint address="" binding="webHttpBinding" contract=“[namanamespace.[namainterface]" behaviorConfiguration=“[namaendpoint]">

<identity><identity>

<dns value=“[hostname]"/>

</identity>

</endpoint>

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

</service>

</services>

Hasil web.config

Hasil web.config (2)

Penting

• Setiap parameter pada REST wajib berupa

String

• Akan lebih baik jika menggunakan method

GET, sehingga function ter-exposeGET, sehingga function ter-expose

• Return function boleh bertipe selain String

Service is running

Hasil REST

Hasil REST

Client - Desktop

• Add service references ke

WS REST yang sudah

dibuat

• Kita akan • Kita akan

mendownloadnya

menggunakan WebClient

• Diparsing, dikonversi, dan

diserialisasi

Kode client

GetMhs

The End

• TAS Open Books

– Dari tengah - akhir

• Pilihan Ganda dan Essay

• Barangsiapa menabur dengan penuh air • Barangsiapa menabur dengan penuh air

mata, akan menuai dengan penuh suka cita!