Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SpringOne 2020 - Building Flexible APIs with Spring HATEOAS

43e9a9d63f7f4f9891c9fcd592b89cfa?s=47 Greg Turnquist
September 03, 2020

SpringOne 2020 - Building Flexible APIs with Spring HATEOAS

Building flexible APIs using hypermedia is easier than ever with Spring HATEOAS 1.1!

Learn how to:

-Build powerful APIs using the simplest tactics

-Configure clients to speak hypermedia with ease.

-Discover its hottest features like affordances, WebFlux support, and new media types.

-Let your API provide everything needed to build HTML forms thanks to HAL-FORMS.

43e9a9d63f7f4f9891c9fcd592b89cfa?s=128

Greg Turnquist

September 03, 2020
Tweet

Transcript

  1. GregLTurnquist.com/springone2020 Building Flexible APIs with Spring HATEOAS September 2–3, 2020

    springone.io 1
  2. GregLTurnquist.com/springone2020 Who am I?

  3. GregLTurnquist.com/springone2020 Who am I?

  4. GregLTurnquist.com/springone2020 Who am I? bit.ly/hacking-with-spring-boot

  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. GregLTurnquist.com/springone2020 Let’s talk about…APIs!

  7. GregLTurnquist.com/springone2020 What is an API?

  8. GregLTurnquist.com/springone2020 What is an API? • Place to get a

    little JSON/XML/whatever?
  9. GregLTurnquist.com/springone2020 What is an API? • Place to get a

    little JSON/XML/whatever? • Way to interact with the app?
  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. 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. GregLTurnquist.com/springone2020 Simple API 6 • Spring MVC • Simple •

    Clear separation between layers
  13. GregLTurnquist.com/springone2020 Simple API 6 • Spring MVC • Simple •

    Clear separation between layers
  14. GregLTurnquist.com/springone2020 Simple API 6 • Spring MVC • Simple •

    Clear separation between layers
  15. GregLTurnquist.com/springone2020 Simple API 6 • Spring MVC • Simple •

    Clear separation between layers
  16. GregLTurnquist.com/springone2020 Simple API 6 • Spring MVC • Simple •

    Clear separation between layers
  17. GregLTurnquist.com/springone2020 Simple API { "id": 0, "name": "Frodo", "role": "ring

    bearer" } 6
  18. GregLTurnquist.com/springone2020 Simple API { "id": 0, "name": "Frodo", "role": "ring

    bearer" } 6 • Data and…
  19. GregLTurnquist.com/springone2020 Simple API { "id": 0, "name": "Frodo", "role": "ring

    bearer" } 6 • Data and… • …nothing else!
  20. GregLTurnquist.com/springone2020 Simple API { "id": 0, "name": "Frodo", "role": "ring

    bearer" } 6 • Data and… • …nothing else! • Some value, but what about…
  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. 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. 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. GregLTurnquist.com/springone2020 What happens when… 6

  25. GregLTurnquist.com/springone2020 What happens when… 6

  26. GregLTurnquist.com/springone2020 What happens when you serve this… { "id": 0,

    "firstName": "Frodo", "lastName": "Baggins", "role": "ring bearer" } 6
  27. GregLTurnquist.com/springone2020 …but older clients try this? 6

  28. GregLTurnquist.com/springone2020 …but older clients try this? 6 • What now?

  29. GregLTurnquist.com/springone2020 …but older clients try this? 6 • What now?

    • Tell users to just use your new API?
  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. 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. 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. 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. GregLTurnquist.com/springone2020 How rough can deprecating APIs be? 6 http://bit.ly/deprecating-apis

  35. GregLTurnquist.com/springone2020 Just do this… 6

  36. GregLTurnquist.com/springone2020 Just do this… 6 • “Never delete a column”

  37. GregLTurnquist.com/springone2020 Just do this… 6 • “Never delete a column”

    • Handle new clients
  38. GregLTurnquist.com/springone2020 Just do this… 6 • “Never delete a column”

    • Handle new clients • Support old clients
  39. GregLTurnquist.com/springone2020 Just do this… 6 • “Never delete a column”

    • Handle new clients • Support old clients • Everyone wins!
  40. GregLTurnquist.com/springone2020 What else do you need?

  41. GregLTurnquist.com/springone2020 What else do you need? • Hypermedia controls

  42. GregLTurnquist.com/springone2020 What else do you need? • Hypermedia controls •

    Instead of telling someone how to use your API on a portal…
  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. 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. 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. 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. 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. GregLTurnquist.com/springone2020 Just migrate from this… 6

  49. GregLTurnquist.com/springone2020 …to this! 6

  50. GregLTurnquist.com/springone2020 …to this! 6

  51. GregLTurnquist.com/springone2020 …to this! 6

  52. GregLTurnquist.com/springone2020 …to this! 6

  53. GregLTurnquist.com/springone2020 …to this! 6

  54. GregLTurnquist.com/springone2020 …to this! 6

  55. GregLTurnquist.com/springone2020 …to this! 6

  56. GregLTurnquist.com/springone2020 …to this! 6

  57. GregLTurnquist.com/springone2020 …to this! 6

  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. 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. 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. 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. 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. 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. GregLTurnquist.com/springone2020 Consuming Hypermedia

  65. GregLTurnquist.com/springone2020 What if…

  66. GregLTurnquist.com/springone2020 What if… • You wanted your client to consume

    hypermedia?
  67. GregLTurnquist.com/springone2020 What if… • You wanted your client to consume

    hypermedia? • Ready to register all those messy message converters?
  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. 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. 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. GregLTurnquist.com/springone2020 RestTemplate support

  72. GregLTurnquist.com/springone2020 RestTemplate support

  73. GregLTurnquist.com/springone2020 RestTemplate support

  74. GregLTurnquist.com/springone2020 RestTemplate support

  75. GregLTurnquist.com/springone2020 RestTemplate support

  76. GregLTurnquist.com/springone2020 We also support WebClient • Inject WebClient.Builder into your

    app • …and .build()!
  77. GregLTurnquist.com/springone2020 We also support WebClient • Inject WebClient.Builder into your

    app • …and .build()!
  78. GregLTurnquist.com/springone2020 We also support WebClient

  79. GregLTurnquist.com/springone2020 We also support WebClient

  80. GregLTurnquist.com/springone2020 We also support WebClient

  81. GregLTurnquist.com/springone2020 We also support WebClient Not in production!

  82. GregLTurnquist.com/springone2020 Additional Features

  83. GregLTurnquist.com/springone2020 Additional Features • Spring WebFlux support

  84. GregLTurnquist.com/springone2020 Additional Features • Spring WebFlux support • More media

    types (UBER+JSON, Collection+JSON, Your Own ™)
  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. 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. 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. 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. 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. 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. 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