Typical way to design and expose HTTP API today is a so called CRUD approach: come up with URL templates for resources, map create-read-update-delete operations to HTTP verbs and serialize domain model as JSON. This approach is nice and easy, but has its severe limitations.
In this talk we'll create an application enhancing its primitive CRUD API all the way to modern, business-centric hypermedia style API using a set of tools from Spring, namely Spring Boot, Spring Data REST, Spring HATEOAS and Spring REST Docs!
10. 10
Method URL Task
POST /ads Create new ad
GET /ads View ads
GET /ads/{id} Get ad
PATCH / PUT /ads/{id} Update ad
DELETE /ads/{id} Delete ad
CRUD Style API
12. CRUD is NOT enough
if (status == Status.NEW) {
publishedAt = LocalDateTime.now();
status = Status.PUBLISHED;
} …
13. 13
Method URL Task
POST /ads/{id}/publishing Publish ad
POST /ads/{id}/expiration Expire ad
GET /ads/search/published Get published ads
API Changes
17. 17
Task Method URL
Update ad PATCH /ads/{id}
Delete ad DELETE /ads/{id}
Publish ad POST /ads/{id}/publishing
Expire ad POST /ads/{id}/expiration
URI Binding & Construction
18. 18
Task Method URL
Update ad
(only if NEW) PATCH /ads/{id}
Delete ad
(only if NEW) DELETE /ads/{id}
Publish ad
(only if NEW) POST /ads/{id}/publishing
Expire ad
(only if
PUBLISHED)
POST /ads/{id}/expiration
"Figuring" Out the Flow
31. Outcomes -
1. Spring Data REST - simple, CRUD-y
and HATEOAS-y; extensible
PRO TIP: won't solve everything
2. Spring MVC - always there to help
3. Link stuff with Spring HATEOAS
4. Document with Spring REST Docs