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

SpringOne 2020 - Building Flexible APIs with Spring HATEOAS

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
    Building Flexible APIs
    with Spring HATEOAS
    September 2–3, 2020
    springone.io
    1

    View full-size slide

  2. GregLTurnquist.com/springone2020
    Who am I?

    View full-size slide

  3. GregLTurnquist.com/springone2020
    Who am I?

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  6. GregLTurnquist.com/springone2020
    Let’s talk about…APIs!

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  9. GregLTurnquist.com/springone2020
    What is an API?
    ● Place to get a little JSON/XML/whatever?
    ● Way to interact with the app?

    View full-size slide

  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?

    View full-size slide

  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!

    View full-size slide

  12. GregLTurnquist.com/springone2020
    Simple API
    6
    ● Spring MVC
    ● Simple
    ● Clear separation between layers

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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?

    View full-size slide

  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?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  26. GregLTurnquist.com/springone2020
    What happens when you serve this…
    {
    "id": 0,
    "firstName": "Frodo",
    "lastName": "Baggins",
    "role": "ring bearer"
    }
    6

    View full-size slide

  27. GregLTurnquist.com/springone2020
    …but older clients try this?
    6

    View full-size slide

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

    View full-size slide

  29. GregLTurnquist.com/springone2020
    …but older clients try this?
    6
    ● What now?
    ● Tell users to just use your new API?

    View full-size slide

  30. GregLTurnquist.com/springone2020
    …but older clients try this?
    6
    ● What now?
    ● Tell users to just use your new API?
    ● Roll out another version?

    View full-size slide

  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?

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  34. GregLTurnquist.com/springone2020
    How rough can deprecating APIs be?
    6
    http://bit.ly/deprecating-apis

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  40. GregLTurnquist.com/springone2020
    What else do you need?

    View full-size slide

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

    View full-size slide

  42. GregLTurnquist.com/springone2020
    What else do you need?
    ● Hypermedia controls
    ● Instead of telling someone how to use your API on a portal…

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  48. GregLTurnquist.com/springone2020
    Just migrate from this…
    6

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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!)

    View full-size slide

  64. GregLTurnquist.com/springone2020
    Consuming Hypermedia

    View full-size slide

  65. GregLTurnquist.com/springone2020
    What if…

    View full-size slide

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

    View full-size slide

  67. GregLTurnquist.com/springone2020
    What if…
    ● You wanted your client to consume hypermedia?
    ● Ready to register all those messy message converters?

    View full-size slide

  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.?

    View full-size slide

  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.?

    View full-size slide

  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!

    View full-size slide

  71. GregLTurnquist.com/springone2020
    RestTemplate support

    View full-size slide

  72. GregLTurnquist.com/springone2020
    RestTemplate support

    View full-size slide

  73. GregLTurnquist.com/springone2020
    RestTemplate support

    View full-size slide

  74. GregLTurnquist.com/springone2020
    RestTemplate support

    View full-size slide

  75. GregLTurnquist.com/springone2020
    RestTemplate support

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  78. GregLTurnquist.com/springone2020
    We also support WebClient

    View full-size slide

  79. GregLTurnquist.com/springone2020
    We also support WebClient

    View full-size slide

  80. GregLTurnquist.com/springone2020
    We also support WebClient

    View full-size slide

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

    View full-size slide

  82. GregLTurnquist.com/springone2020
    Additional Features

    View full-size slide

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

    View full-size slide

  84. GregLTurnquist.com/springone2020
    Additional Features
    ● Spring WebFlux support
    ● More media types (UBER+JSON, Collection+JSON, Your Own ™)

    View full-size slide

  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)

    View full-size slide

  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

    View full-size slide

  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!

    View full-size slide

  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!

    View full-size slide

  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!

    View full-size slide

  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!

    View full-size slide

  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

    View full-size slide