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

SpringOne 2020 - Building Flexible APIs with Sp...

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.

Greg Turnquist

September 03, 2020
Tweet

More Decks by Greg Turnquist

Other Decks in Technology

Transcript

  1. 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
  2. GregLTurnquist.com/springone2020 What is an API? • Place to get a

    little JSON/XML/whatever? • Way to interact with the app?
  3. 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?
  4. 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!
  5. GregLTurnquist.com/springone2020 Simple API { "id": 0, "name": "Frodo", "role": "ring

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

    bearer" } 6 • Data and… • …nothing else! • Some value, but what about… • …updates?
  7. 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?
  8. 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?
  9. GregLTurnquist.com/springone2020 What happens when you serve this… { "id": 0,

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

    • Tell users to just use your new API? • Roll out another version?
  11. 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?
  12. 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
  13. 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 “
  14. GregLTurnquist.com/springone2020 Just do this… 6 • “Never delete a column”

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

    Instead of telling someone how to use your API on a portal…
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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!)
  27. GregLTurnquist.com/springone2020 What if… • You wanted your client to consume

    hypermedia? • Ready to register all those messy message converters?
  28. 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.?
  29. 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.?
  30. 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!
  31. GregLTurnquist.com/springone2020 Additional Features • Spring WebFlux support • More media

    types (UBER+JSON, Collection+JSON, Your Own ™) • Standardized errors with Problem+JSON (RFC-7807)
  32. 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
  33. 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!
  34. 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!
  35. 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!
  36. 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!
  37. 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