Design REST-ful Web Service

1,225 views

Published on

Design REST-ful Web Service, Use Spring MVC as Example

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,225
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
8
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Design REST-ful Web Service

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

×