Your SlideShare is downloading. ×
Design Restful Web Service, use SpringMVC as Example
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Design Restful Web Service, use SpringMVC as Example

2,469

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,469
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
60
Comments
0
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Design REST-ful Web Service
    - Use Spring MVC as Example
    kevingo
  • 2. REpresentationalState Transfer
    Apache co-founder
    Apache HTTP Server project developer
    HTTP 1.0 / 1.1 spec author
    Roy T. Fielding
    http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm
  • 3. A style of software architecture for distributed hypermedia systems.
  • 4. REST 是一種軟體架構風格適合用來開發Web Service
  • 5. WHO USE REST
  • 6. WHO USE REST
  • 7. 向主流靠攏!
  • 8. 萬物皆資源
    Any information that can be named can be a resource
    { 'obj1': {  'child1':'value1',   'child2':'value2'  },  'array1': [1, 2, 3, 4, 5]}
    <People>
    <name>Fu</name>
    <Age>10</Age>
    </People>
  • 9. 任何資源都用URL表達
    http://itri.com/proj/i236
    http://itri.com/people/kevingo
  • 10. 相同資源,格式可能不同
    http://xxx.com/article
  • 11. 回想REST的全名…
    REpresentational
    對於資源在特定時刻的狀態的描述
    State
    資源的狀態
    Transfer
    資源在ClientSide和Server Side之間的轉移
  • 12. REST 說穿了 …
    Service Provider
    我要 XXX
    透過HTTP
    這是 OOO
  • 13. RESTful ?
  • 14. RESTful …
    forget -> forgetful : 忘記 -> 健忘的
    color -> colorful : 顏色 -> 多顏色的
    peace -> peaceful : 和平 -> 和平的
    ….
  • 15. 同理可證
  • 16. REST->RESTful
    符合REST規範的
  • 17. REST到底規範了什麼?
  • 18. 使用URL來表達資源
    工研院的i236計畫
    http://itri.org/project/i236
    參與工研院paas計畫的人員
    http://itri.org/project/paas/people
  • 19. 利用HTTP定義的動詞方法來進行資源的操作
    GET
    POST
    PUT
    DELETE
  • 20. Communicate Stateless
  • 21. 其他還有…
    Cacheable
    Client–server
    Layered system
    Code on demand
  • 22. RESTful URL design
  • 23. http://itri.org/addProject?name=i236
    http://itri.org/showProject?name=i236
    http://itri.org/deleteProject?name=i236
    http://itri.org/editProject?name=i236
  • 24. GET
    http://itri.org/project/i236
    DELETE
    POST
    PUT
  • 25. REST的優點
    Scalability - stateless
    搜尋引擎喜歡Clean URL
    統一的存取介面 –HTTPMethod
    使用HTTP Catch和proxy server提高負載程度
  • 26. 關於HTTP方法我們可以懂得更多
  • 27.
  • 28. Spring MVC
    REST in Java
    Spring 3.0
  • 29. annotation-based programming model
  • 30. 使用annotation減少繼承宣告
    配合context:component-scan
    減少XML設定文件
  • 31. @Controller
    聲明這是一個Controller類別
  • 32. @Controller
    public class ProjectController {…}
    取代
    public class ProjectController extends xxxController {…}
  • 33. @RequestMapping
    將請求轉交至對應的類別或方法
  • 34. 可以用在類別上
    @Controller
    @RequestMapping(value=“/projects”)
    public class ProjectController {…}
    也可以用在方法上
    @RequestMapping(value=“/projects”)
    public ModelAndView show() {…}
  • 35. 還可以指定請求的方法
    @RequestMapping(value=“/projects”, method=RequestMethod.POST)
    public ModelAndView show() {…}
    連請求的header都可以指定
    @RequestMapping(headers=“content-type=text/*”)
    public ModelAndView show() {…}
  • 36. @PathVariable
    將URL的參數和方法的參數
    進行綁定
  • 37. URL參數會傳入方法中
    @RequestMapping(“/project/{name}”)
    public ModelAndView show(@PathVariable String name) {…}
  • 38. 萬事俱備只欠東風
  • 39. 瀏覽器只支援GET和POST
  • 40. 貼心的Spring幫我們解決…
    在web.xml加入HiddenHttpMethodFilter
    1
    使用spring form tag來指定http method
    (PUT or DELETE)
    2
    其實是用POST發出請求,只是偷偷塞了一個hidden field來放真正的HTTP Method
    3
    HiddenHttpMethodFilter會根據真正的HTTP Method幫我們轉發到正確的Controller
  • 41. <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>
  • 42. 偷偷看一下原始碼…
  • 43. 還有什麼?
  • 44. 每次要使用REST都要產生一個form?
  • 45. 使用RestTemplate類別輕鬆呼叫RESTResources
    謝謝Spring 3.0
  • 46. 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
  • 47. 還有一點點問題…
  • 48. 怎麼做?
  • 49. 三種作法
    在Request Header裡面宣告
    GET /project/i236 HTTP/1.1
    Accept: text/html
    GET/project/i236 HTTP/1.1
    Accept: application/json
    • 使用副檔名
    • 50. 使用額外參數
    /project/i236.json
    /project/i236.html
    /project/i236?format=json
    /project/i236?format=html
  • 51. ContentNegotiatingViewResolver
    支援瀏覽器Request Header
    支援副檔名
  • 52. 瀏覽器Request Header
    不同的瀏覽器支援的格式不同
  • 使用不同副檔名
    引入 bean - ContentNegotiatingViewResolver
    1
    增加mediaTypesproperty
    2
    在mediaTypes中,用map填入想要的附檔名
    3
    在defaultViews中,實作要呈現的View
  • 67. 看一下DEMO吧…
  • 68. 給你魚竿 - 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/
  • 69. 給你魚竿 - 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/
  • 70. curl -X DELETE
    http://itri.org/ReadingClub/kevingo/3
    Thank you 

×