54
Design REST-ful Web Service kevingo - Use Spring MVC as Example

Design Restful Web Service, use SpringMVC as Example

  • Upload
    kevingo

  • View
    2.847

  • Download
    1

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Design Restful Web Service, use SpringMVC as Example

Design REST-ful Web Service

kevingo

- Use Spring MVC as Example

Page 2: Design Restful Web Service, use SpringMVC as Example

REpresentational State Transfer

Roy T. Fieldinghttp://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

Apache co-founderApache HTTP Server project developerHTTP 1.0 / 1.1 spec author

Page 3: Design Restful Web Service, use SpringMVC as Example

A style of software architecture for distributed hypermedia systems.

Page 4: Design Restful Web Service, use SpringMVC as Example

REST 是一種軟體架構風格適合用來開發 Web

Service

Page 5: Design Restful Web Service, use SpringMVC as Example

WHO USE REST

Page 6: Design Restful Web Service, use SpringMVC as Example

WHO USE REST

Page 7: Design Restful Web Service, use SpringMVC as Example

向主流靠攏 !

Page 8: Design Restful Web Service, use SpringMVC as Example

萬物皆資源Any information that can be named can be a resource

<People> <name>Fu</name> <Age>10</Age></People>

{ 'obj1': { 'child1':'value1', 'child2':'value2' }, 'array1': [1, 2, 3, 4, 5]}

Page 9: Design Restful Web Service, use SpringMVC as Example

任何資源都用 URL 表達http://itri.com/proj/i236

http://itri.com/people/kevingo

Page 10: Design Restful Web Service, use SpringMVC as Example

相同資源,格式可能不同http://xxx.com/article

Page 11: Design Restful Web Service, use SpringMVC as Example

回想 REST 的全名…

REpresentational

State

Transfer

對於資源在特定時刻的狀態的描述

資源在 Client Side 和 Server Side 之間的轉移

資源的狀態

Page 12: Design Restful Web Service, use SpringMVC as Example

REST 說穿了 …

Service Provide

r

我要 XXX

這是 OOO

透過 HTTP

Page 13: Design Restful Web Service, use SpringMVC as Example

RESTful ?

Page 14: Design Restful Web Service, use SpringMVC as Example

RESTful …• forget -> forgetful : 忘記 -> 健忘的• color -> colorful : 顏色 -> 多顏色的• peace -> peaceful : 和平 -> 和平的• ….

Page 15: Design Restful Web Service, use SpringMVC as Example

同理可證

Page 16: Design Restful Web Service, use SpringMVC as Example

REST -> RESTful

符合 REST 規範的

Page 17: Design Restful Web Service, use SpringMVC as Example

REST 到底規範了什麼?

Page 18: Design Restful Web Service, use SpringMVC as Example

使用 URL 來表達資源

http://itri.org/project/i236

http://itri.org/project/paas/people

工研院的 i236 計畫

參與工研院 paas 計畫的人員

Page 19: Design Restful Web Service, use SpringMVC as Example

利用 HTTP 定義的動詞方法來進行資源的操作

GETPUT

POSTDELETE

Page 20: Design Restful Web Service, use SpringMVC as Example

Communicate Stateless

Page 21: Design Restful Web Service, use SpringMVC as Example

其他還有…• Cacheable

• Client–server

• Layered system

• Code on demand

Page 22: Design Restful Web Service, use SpringMVC as Example

RESTful URL design

Page 23: Design Restful Web Service, use SpringMVC as Example

http://itri.org/addProject?name=i236http://itri.org/showProject?name=i236http://itri.org/deleteProject?name=i236http://itri.org/editProject?name=i236

Page 24: Design Restful Web Service, use SpringMVC as Example

http://itri.org/project/i236GETDELETEPOSTPUT

Page 25: Design Restful Web Service, use SpringMVC as Example

REST 的優點• Scalability - stateless

• 搜尋引擎喜歡 Clean URL

• 統一的存取介面 – HTTP Method

• 使用 HTTP Catch 和 proxy server 提高負載程度

Page 26: Design Restful Web Service, use SpringMVC as Example

REST in JavaSpring MVC

Spring 3.0

Page 27: Design Restful Web Service, use SpringMVC as Example

annotation-based programming model

Page 28: Design Restful Web Service, use SpringMVC as Example

使用 annotation 減少繼承宣告

配合 context:component-scan減少 XML 設定文件

Page 29: Design Restful Web Service, use SpringMVC as Example

@Controller

聲明這是一個 Controller 類別

Page 30: Design Restful Web Service, use SpringMVC as Example

@Controllerpublic class ProjectController {…}

public class ProjectController extends xxxController {…}

取代

Page 31: Design Restful Web Service, use SpringMVC as Example

@RequestMapping

將請求轉交至對應的類別或方法

Page 32: Design Restful Web Service, use SpringMVC as Example

@Controller@RequestMapping(value=“/projects”)public class ProjectController {…}

@RequestMapping(value=“/projects”)public ModelAndView show() {…}

可以用在類別上

也可以用在方法上

Page 33: Design Restful Web Service, use SpringMVC as Example

還可以指定請求的方法@RequestMapping(value=“/projects”, method=RequestMethod.POST)public ModelAndView show() {…}

連請求的 header 都可以指定@RequestMapping(headers=“content-type=text/*”)public ModelAndView show() {…}

Page 34: Design Restful Web Service, use SpringMVC as Example

@PathVariable

將 URL 的參數和方法的參數進行綁定

Page 35: Design Restful Web Service, use SpringMVC as Example

URL 參數會傳入方法中@RequestMapping(“/project/{name}”)public ModelAndView show(@PathVariable String name) {…}

Page 36: Design Restful Web Service, use SpringMVC as Example

萬事俱備只欠東風

Page 37: Design Restful Web Service, use SpringMVC as Example

瀏覽器只支援GET 和POST

Page 38: Design Restful Web Service, use SpringMVC as Example

貼心的 Spring 幫我們解決…

1

2

在 web.xml 加入 HiddenHttpMethodFilter

使用 spring form tag 來指定 http method

(PUT or DELETE)

其實是用 POST 發出請求,只是偷偷塞了一個hidden field 來放真正的 HTTP Method

3 HiddenHttpMethodFilter 會根據真正的 HTTP Method 幫我們轉發到正確的 Controller

Page 39: Design Restful Web Service, use SpringMVC as Example

<form:form action="project" method="delete"> <input type="submit" value="Delete Project" name=“i236" /></form:form>

<form id="command" action="project/i236" method="post"><input type="hidden" name="_method" value="delete"/> <input type="submit" value="Delete i236" name="i236" />

</form>

Page 40: Design Restful Web Service, use SpringMVC as Example

偷偷看一下原始碼…

Page 41: Design Restful Web Service, use SpringMVC as Example

還有什麼?

Page 42: Design Restful Web Service, use SpringMVC as Example

每次要使用 REST都要產生一個

form ?

Page 43: Design Restful Web Service, use SpringMVC as Example

使用 RestTemplate 類別

輕鬆呼叫 REST Resources 謝謝 Spring 3.0

Page 44: Design Restful Web Service, use SpringMVC as Example

• GET – getForObject• POST – postForObject• PUT – put• DELETE – delete

…..

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html

Page 45: Design Restful Web Service, use SpringMVC as Example

還有一點點問題…

Page 46: Design Restful Web Service, use SpringMVC as Example

怎麼做?

Page 47: Design Restful Web Service, use SpringMVC as Example

三種作法• 在 Request Header 裡面宣告

GET /project/i236 HTTP/1.1Accept: text/html

GET /project/i236 HTTP/1.1Accept: application/json

• 使用副檔名

• 使用額外參數

/project/i236.json/project/i236.html

/project/i236?format=json/project/i236?format=html

Page 48: Design Restful Web Service, use SpringMVC as Example

ContentNegotiatingViewResolver

支援瀏覽器Request Header

支援副檔名

Page 49: Design Restful Web Service, use SpringMVC as Example

瀏覽器 Request Header

• 不同的瀏覽器支援的格式不同

• text/html• application/xhtml+xml• application/xml

• application/xml• application/xhtml+xml• text/html• text/plain• Image/png

• iamge/jpeg• application/xaml+xml• application/x-ms-application• image/gif• Image/pjpeg• application/x-ms-xbap• ……

Page 50: Design Restful Web Service, use SpringMVC as Example

使用不同副檔名1

2

引入 bean - ContentNegotiatingViewResolver

增加 mediaTypes property

在 mediaTypes 中,用 map 填入想要的附檔名

3 在 defaultViews 中,實作要呈現的 View

Page 51: Design Restful Web Service, use SpringMVC as Example

看一下 DEMO 吧…

Page 52: Design Restful Web Service, use SpringMVC as Example

給你魚竿 - REST• Roy Thomas Fielding 博士論文

– http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

• REST-ful URI design– http://redrata.com/restful-uri-design/

• REST 簡介– http://www.shlug.org/Slide/2011/2/REST.pdf

• RESTful Rails Development– http://www.b-simple.de/download/restful_rails_en.pdf

• REST (Representational State Transfer) and RESTful

web services: Methods, Concepts and Examples– http://mauriziostorani.wordpress.com/2008/07/27/rest-representational-state-transfer-

and-restful-web-services-concepts-and-examples/

Page 53: Design Restful Web Service, use SpringMVC as Example

給你魚竿 - Spring MVC REST

• Spring API– http://static.springsource.org/spring/docs/3.0.x/javadoc-api/

• Spring MVC Rest– http://www.slideshare.net/habuma/spring-mvc-rest

• REST in Spring 3: @MVC– http://blog.springsource.com/2009/03/08/rest-in-spring-3-

mvc/

Page 54: Design Restful Web Service, use SpringMVC as Example

http://itri.org/ReadingClub/kevingo/3curl -X DELETE

Thank you