Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Building Flexible APIs with Spring HATEOAS

234 Aufrufe

Veröffentlicht am

SpringOne 2020

Greg Turnquist: Principal Software Engineer, VMware

Veröffentlicht in: Software
  • Als Erste(r) kommentieren

Building Flexible APIs with Spring HATEOAS

  1. 1. GregLTurnquist.com/springone2020 Building Flexible APIs with Spring HATEOAS September 2–3, 2020 springone.io 1
  2. 2. GregLTurnquist.com/springone2020 Who am I?
  3. 3. GregLTurnquist.com/springone2020 Who am I?
  4. 4. GregLTurnquist.com/springone2020 Who am I? bit.ly/hacking-with-spring-boot
  5. 5. GregLTurnquist.com/springone2020 Who am I? bit.ly/hacking-with-spring-boot ● Principal developer on the Spring team ● Nashville JUG co-founder ● Committer & Project Lead for several Spring projects ● YouTube/GregTurnquist
  6. 6. GregLTurnquist.com/springone2020 Let’s talk about…APIs!
  7. 7. GregLTurnquist.com/springone2020 What is an API?
  8. 8. GregLTurnquist.com/springone2020 What is an API? ● Place to get a little JSON/XML/whatever?
  9. 9. GregLTurnquist.com/springone2020 What is an API? ● Place to get a little JSON/XML/whatever? ● Way to interact with the app?
  10. 10. GregLTurnquist.com/springone2020 What is an API? ● Place to get a little JSON/XML/whatever? ● Way to interact with the app? ● Means to extract value from another app to help your own app?
  11. 11. GregLTurnquist.com/springone2020 What is an API? ● Place to get a little JSON/XML/whatever? ● Way to interact with the app? ● Means to extract value from another app to help your own app? Let’s dig in and discover what it means to have a flexible API!
  12. 12. GregLTurnquist.com/springone2020 Simple API 6 ● Spring MVC ● Simple ● Clear separation between layers
  13. 13. GregLTurnquist.com/springone2020 Simple API 6 ● Spring MVC ● Simple ● Clear separation between layers
  14. 14. GregLTurnquist.com/springone2020 Simple API 6 ● Spring MVC ● Simple ● Clear separation between layers
  15. 15. GregLTurnquist.com/springone2020 Simple API 6 ● Spring MVC ● Simple ● Clear separation between layers
  16. 16. GregLTurnquist.com/springone2020 Simple API 6 ● Spring MVC ● Simple ● Clear separation between layers
  17. 17. GregLTurnquist.com/springone2020 Simple API { "id": 0, "name": "Frodo", "role": "ring bearer" } 6
  18. 18. GregLTurnquist.com/springone2020 Simple API { "id": 0, "name": "Frodo", "role": "ring bearer" } 6 ● Data and…
  19. 19. GregLTurnquist.com/springone2020 Simple API { "id": 0, "name": "Frodo", "role": "ring bearer" } 6 ● Data and… ● …nothing else!
  20. 20. GregLTurnquist.com/springone2020 Simple API { "id": 0, "name": "Frodo", "role": "ring bearer" } 6 ● Data and… ● …nothing else! ● Some value, but what about…
  21. 21. GregLTurnquist.com/springone2020 Simple API { "id": 0, "name": "Frodo", "role": "ring bearer" } 6 ● Data and… ● …nothing else! ● Some value, but what about… ● …updates?
  22. 22. GregLTurnquist.com/springone2020 Simple API { "id": 0, "name": "Frodo", "role": "ring bearer" } 6 ● Data and… ● …nothing else! ● Some value, but what about… ● …updates? ● …breaking changes?
  23. 23. GregLTurnquist.com/springone2020 Simple API { "id": 0, "name": "Frodo", "role": "ring bearer" } 6 ● Data and… ● …nothing else! ● Some value, but what about… ● …updates? ● …breaking changes? ● …backwards compatibility?
  24. 24. GregLTurnquist.com/springone2020 What happens when… 6
  25. 25. GregLTurnquist.com/springone2020 What happens when… 6
  26. 26. GregLTurnquist.com/springone2020 What happens when you serve this… { "id": 0, "firstName": "Frodo", "lastName": "Baggins", "role": "ring bearer" } 6
  27. 27. GregLTurnquist.com/springone2020 …but older clients try this? 6
  28. 28. GregLTurnquist.com/springone2020 …but older clients try this? 6 ● What now?
  29. 29. GregLTurnquist.com/springone2020 …but older clients try this? 6 ● What now? ● Tell users to just use your new API?
  30. 30. GregLTurnquist.com/springone2020 …but older clients try this? 6 ● What now? ● Tell users to just use your new API? ● Roll out another version?
  31. 31. GregLTurnquist.com/springone2020 …but older clients try this? 6 ● What now? ● Tell users to just use your new API? ● Roll out another version? ● Handle this AND the new format?
  32. 32. GregLTurnquist.com/springone2020 What about versioning? 6 https://www.infoq.com/news/2013/12/api-versioning/ ● Knot = Single version of API ● P2P = Multiple versions ● Compatible = One API supports multiple versions
  33. 33. GregLTurnquist.com/springone2020 What about versioning? 6 It is always possible for some unexpected reason to come along that requires a completely different API, especially when the semantics of the interface change or security issues require the abandonment of previously deployed software. My point was that there is no need to anticipate such world-breaking changes with a version ID. We have the hostname for that. What you are creating is not a new version of the API, but a new system with a new brand. On the Web, we call that a new website. Websites don’t come with version numbers attached because they never need to. Neither should a RESTful API. A RESTful API (done right) is just a website for clients with a limited vocabulary. —Dr. Roy Fielding “
  34. 34. GregLTurnquist.com/springone2020 How rough can deprecating APIs be? 6 http://bit.ly/deprecating-apis
  35. 35. GregLTurnquist.com/springone2020 Just do this… 6
  36. 36. GregLTurnquist.com/springone2020 Just do this… 6 ● “Never delete a column”
  37. 37. GregLTurnquist.com/springone2020 Just do this… 6 ● “Never delete a column” ● Handle new clients
  38. 38. GregLTurnquist.com/springone2020 Just do this… 6 ● “Never delete a column” ● Handle new clients ● Support old clients
  39. 39. GregLTurnquist.com/springone2020 Just do this… 6 ● “Never delete a column” ● Handle new clients ● Support old clients ● Everyone wins!
  40. 40. GregLTurnquist.com/springone2020 What else do you need?
  41. 41. GregLTurnquist.com/springone2020 What else do you need? ● Hypermedia controls
  42. 42. GregLTurnquist.com/springone2020 What else do you need? ● Hypermedia controls ● Instead of telling someone how to use your API on a portal…
  43. 43. GregLTurnquist.com/springone2020 What else do you need? ● Hypermedia controls ● Instead of telling someone how to use your API on a portal… ● …give them the controls right in the API
  44. 44. GregLTurnquist.com/springone2020 What else do you need? ● Hypermedia controls ● Instead of telling someone how to use your API on a portal… ● …give them the controls right in the API ● …using standard media types
  45. 45. GregLTurnquist.com/springone2020 What else do you need? ● Hypermedia controls ● Instead of telling someone how to use your API on a portal… ● …give them the controls right in the API ● …using standard media types ● It’s how the web functions
  46. 46. GregLTurnquist.com/springone2020 What else do you need? ● Hypermedia controls ● Instead of telling someone how to use your API on a portal… ● …give them the controls right in the API ● …using standard media types ● It’s how the web functions ● It’s the reason the web succeeded
  47. 47. GregLTurnquist.com/springone2020 What else do you need? ● Hypermedia controls ● Instead of telling someone how to use your API on a portal… ● …give them the controls right in the API ● …using standard media types ● It’s how the web functions ● It’s the reason the web succeeded ● The reason everyone today builds web apps
  48. 48. GregLTurnquist.com/springone2020 Just migrate from this… 6
  49. 49. GregLTurnquist.com/springone2020 …to this! 6
  50. 50. GregLTurnquist.com/springone2020 …to this! 6
  51. 51. GregLTurnquist.com/springone2020 …to this! 6
  52. 52. GregLTurnquist.com/springone2020 …to this! 6
  53. 53. GregLTurnquist.com/springone2020 …to this! 6
  54. 54. GregLTurnquist.com/springone2020 …to this! 6
  55. 55. GregLTurnquist.com/springone2020 …to this! 6
  56. 56. GregLTurnquist.com/springone2020 …to this! 6
  57. 57. GregLTurnquist.com/springone2020 …to this! 6
  58. 58. GregLTurnquist.com/springone2020 { "id": 0, "firstName": "Frodo", "lastName": "Baggins", "role": "ring bearer", "name": "Frodo Baggins", "_links": { "self": { "href": "http://localhost:8080/rest/employees/0" }, "employees": { "href": "http://localhost:8080/rest/employees" } }, … So you can do this! 6 ● New clients
  59. 59. GregLTurnquist.com/springone2020 { "id": 0, "firstName": "Frodo", "lastName": "Baggins", "role": "ring bearer", "name": "Frodo Baggins", "_links": { "self": { "href": "http://localhost:8080/rest/employees/0" }, "employees": { "href": "http://localhost:8080/rest/employees" } }, … So you can do this! 6 ● New clients ● Old clients
  60. 60. GregLTurnquist.com/springone2020 { "id": 0, "firstName": "Frodo", "lastName": "Baggins", "role": "ring bearer", "name": "Frodo Baggins", "_links": { "self": { "href": "http://localhost:8080/rest/employees/0" }, "employees": { "href": "http://localhost:8080/rest/employees" } }, … So you can do this! 6 ● New clients ● Old clients ● Navigate between related components
  61. 61. GregLTurnquist.com/springone2020 …and this 6 … "_templates": { "default": { "method": "put", "properties": [ { "name": "firstName" }, { "name": "id", "readOnly": true }, { "name": "lastName" }, { "name": "name" }, { "name": "role" } ]}}} ● New clients ● Old clients ● Navigate between related components ● Effect change
  62. 62. GregLTurnquist.com/springone2020 … "_templates": { "default": { "method": "put", "properties": [ { "name": "firstName" }, { "name": "id", "readOnly": true }, { "name": "lastName" }, { "name": "name" }, { "name": "role" } ]}}} …and this 6 ● New clients ● Old clients ● Navigate between related components ● Effect change
  63. 63. GregLTurnquist.com/springone2020 … "_templates": { "default": { "method": "put", "properties": [ { "name": "firstName" }, { "name": "id", "readOnly": true }, { "name": "lastName" }, { "name": "name" }, { "name": "role" } ]}}} …and this 6 ● New clients ● Old clients ● Navigate between related components ● Effect change (even for old clients!)
  64. 64. GregLTurnquist.com/springone2020 Consuming Hypermedia
  65. 65. GregLTurnquist.com/springone2020 What if…
  66. 66. GregLTurnquist.com/springone2020 What if… ● You wanted your client to consume hypermedia?
  67. 67. GregLTurnquist.com/springone2020 What if… ● You wanted your client to consume hypermedia? ● Ready to register all those messy message converters?
  68. 68. GregLTurnquist.com/springone2020 What if… ● You wanted your client to consume hypermedia? ● Ready to register all those messy message converters? ● Do you really want to look up how to customize RestTemplate (or WebClient) to handle HAL, HAL-FORMS, etc., etc., etc.?
  69. 69. GregLTurnquist.com/springone2020 What if… ● You wanted your client to consume hypermedia? ● Ready to register all those messy message converters? ● Do you really want to look up how to customize RestTemplate (or WebClient) to handle HAL, HAL-FORMS, etc., etc., etc.?
  70. 70. GregLTurnquist.com/springone2020 What if… ● You wanted your client to consume hypermedia? ● Ready to register all those messy message converters? ● Do you really want to look up how to customize RestTemplate (or WebClient) to handle HAL, HAL-FORMS, etc., etc., etc.? Spring HATEOAS has you covered!
  71. 71. GregLTurnquist.com/springone2020 RestTemplate support
  72. 72. GregLTurnquist.com/springone2020 RestTemplate support
  73. 73. GregLTurnquist.com/springone2020 RestTemplate support
  74. 74. GregLTurnquist.com/springone2020 RestTemplate support
  75. 75. GregLTurnquist.com/springone2020 RestTemplate support
  76. 76. GregLTurnquist.com/springone2020 We also support WebClient ● Inject WebClient.Builder into your app ● …and .build()!
  77. 77. GregLTurnquist.com/springone2020 We also support WebClient ● Inject WebClient.Builder into your app ● …and .build()!
  78. 78. GregLTurnquist.com/springone2020 We also support WebClient
  79. 79. GregLTurnquist.com/springone2020 We also support WebClient
  80. 80. GregLTurnquist.com/springone2020 We also support WebClient
  81. 81. GregLTurnquist.com/springone2020 We also support WebClient 😳Not in production!
  82. 82. GregLTurnquist.com/springone2020 Additional Features
  83. 83. GregLTurnquist.com/springone2020 Additional Features ● Spring WebFlux support
  84. 84. GregLTurnquist.com/springone2020 Additional Features ● Spring WebFlux support ● More media types (UBER+JSON, Collection+JSON, Your Own ™)
  85. 85. GregLTurnquist.com/springone2020 Additional Features ● Spring WebFlux support ● More media types (UBER+JSON, Collection+JSON, Your Own ™) ● Standardized errors with Problem+JSON (RFC-7807)
  86. 86. GregLTurnquist.com/springone2020 Additional Features ● Spring WebFlux support ● More media types (UBER+JSON, Collection+JSON, Your Own ™) ● Standardized errors with Problem+JSON (RFC-7807) ● Lots of performance improvements
  87. 87. GregLTurnquist.com/springone2020 Additional Features ● Spring WebFlux support ● More media types (UBER+JSON, Collection+JSON, Your Own ™) ● Standardized errors with Problem+JSON (RFC-7807) ● Lots of performance improvements ● Increasing community involvement ● JSON:API, Siren media types ● Document updates ● New ideas!
  88. 88. GregLTurnquist.com/springone2020 Additional Features ● Spring WebFlux support ● More media types (UBER+JSON, Collection+JSON, Your Own ™) ● Standardized errors with Problem+JSON (RFC-7807) ● Lots of performance improvements ● Increasing community involvement ● JSON:API, Siren media types ● Document updates ● New ideas!
  89. 89. GregLTurnquist.com/springone2020 Additional Features ● Spring WebFlux support ● More media types (UBER+JSON, Collection+JSON, Your Own ™) ● Standardized errors with Problem+JSON (RFC-7807) ● Lots of performance improvements ● Increasing community involvement ● JSON:API, Siren media types ● Document updates ● New ideas!
  90. 90. GregLTurnquist.com/springone2020 Additional Features ● Spring WebFlux support ● More media types (UBER+JSON, Collection+JSON, Your Own ™) ● Standardized errors with Problem+JSON (RFC-7807) ● Lots of performance improvements ● Increasing community involvement ● JSON:API, Siren media types ● Document updates ● New ideas! Check it out!
  91. 91. GregLTurnquist.com/springone2020 Thank you SpringOne 2020! Join me on #session-building-flexible-apis-with-spring-hateoas for Q&A Follow us on twitter @SpringHATEOAS Visit GregLTurnquist.com/springone2020 to WIN a paperback+ebook copy of Hacking with Spring Boot 2.3

×