The Power of REST - Greg L. Turnquist
The Power of REST - Greg L. Turnquist
Greg L. Turnquist
The Power of REST
Every effort has been made in the preparation of this book to ensure
the accuracy of the information presented. However, the information
contained in this book is sold without warranty, either express or
implied. Neither the author, nor its dealers and distributors will be
held liable for any damages caused or alleged to be caused directly or
indirectly by this book.
All opinions stated are the author’s and do not necessarily reflect those
of the Spring team, Pivotal Software, VMware, Dell EMC, or any other
entity. Any usage of "Spring", "Spring HATEOAS", Spring Boot", "Spring
Framework", "Project Reactor", any other portfolio project, "Pivotal",
"VMware", or any other entity is completely unaffiliated with its
owners. Any excerpts and usages thereof are strictly in the spirit of
"fair use."
Certain outputs have been edited to fit the format of this book. Nothing
of critical value has been left out on purpose.
Greg L. Turnquist
c/o The Power of REST
P.O. Box 4042
1
Clarksville, TN 37043
USA
ISBN: 123-45-6789
GregLTurnquist.com/the-power-of-rest
2
Dedicated to my tech friends that LOVE geeking out.
3
4
About the Author
Greg L. Turnquist works on the Spring team as a principal
developer at VMware. He is a committer to Spring HATEOAS,
Spring Data, Spring Boot, R2DBC, and Spring Session for
MongoDB. He also wrote Packt’s best-selling title, Learning
Spring Boot 2.0 2nd Edition. He co-founded the Nashville Java
User Group in 2010 and hasn’t met a Java app (yet) that he
doesn’t like.
5
6
Table of Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1 ......................................................9
Straw man arguments against REST . . . . . . . . . . . . . . . . . . . . 9
REST requires lots of hops . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Rest servces too much data . . . . . . . . . . . . . . . . . . . . . . . . . 11
Just put SQL in the client already!. . . . . . . . . . . . . . . . . . . 12
What problem are you solving?. . . . . . . . . . . . . . . . . . . . . 13
2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
What does REST really do? . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
The Basis of REST. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Evolving an API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Honoring the robustness principle . . . . . . . . . . . . . . . . . . 22
3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Getting the real message of REST out there. . . . . . . . . . . . . 23
How the web does it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Don’t argue the wrong end of things . . . . . . . . . . . . . . . . 26
Don’t preach it, use it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
7
8
Straw man arguments against REST
1
Straw man arguments against
REST
I was kind of shocked when I saw Yet Another Posted
Solution to REST. I sighed and commented, and drew the ire
of many. So I figured this might be a good time to gather
some thoughts on REST.
9
Straw man arguments against REST
This is the classic problem when you run into when fetching
a 3NF (3rd Normal Form) data structure served up as REST.
10
Straw man arguments against REST
11
Straw man arguments against REST
12
Straw man arguments against REST
13
Straw man arguments against REST
What does REST solve? REST solves the brittle problem that
arose with CORBA and SOAP.
Think about that. When web sites make updates, does the
web browser require an update? And why?
14
Straw man arguments against REST
15
Straw man arguments against REST
16
What does REST really do?
2
What does REST really do?
In the previous chapter, I challenged someone’s proposal
that their client-side query language could supplant the
power of REST. It seemed to attack straw man arguments
about REST. In this chapter, let’s delve a little more into what
REST does and why it does it.
REST was created to take the concepts that made the web
successful, into API development. The success of the web,
which some people don’t realize, can be summarized like
this: “if the web page is updated, does the browser need an
update?”
No.
17
What does REST really do?
limit, and still get money. But when things are made
consistent, it’s you that will be paying the cost of
overdrawing, not the bank.
Updating ALL the clients because you want to split your API’s
“name” field into “firstName” and “lastName” will be nixed
by management until you A) show that it doesn’t impact
business or B) prove the downtime to upgrade won’t cause
any loss of revenue.
Evolving an API
18
What does REST really do?
features. But only when they’re good and ready. And SOAP
and CORBA were not built for this. Their definition
languages (WSDL and IDL) don’t know how to be “flexible”.
But REST can. How? Let’s start with that example mentioned
earlier, a resource that serves up a name. Perhaps a small
piece of some e-commerce platform. You design this:
Example 1. Foo
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Customer {
private String name;
}
Example 2. Foo
{
"name": "Frodo Baggins",
"_links": {
"self": {
"href": "/customers/1"
}
}
}
This nice bit of JSON flies around the system. You build
19
What does REST really do?
You nod and get cracking. Just need to replace name with
firstName and lastName, and update all the clients. Except
what you just said will incur downtime. No, you need
something a little smoother. Something that can roll out and
not impact the existing clients. Instead of “versioning” your
API, why not ADD TO your existing resource?
Example 3. Foo
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Customer {
Ta-dah!
20
What does REST really do?
Example 4. Foo
{
"firstName": "Frodo",
"lastName": "Baggins",
"name": "Frodo Baggins",
"_links": {
"self": {
"href": "/customers/1"
}
}
}
You are now sporting both the new fields and the original
one. Old clients, if they follow the conventions of REST, will
simply ignore the new stuff and use the old stuff. If they
need to POST an update, they know where to send it and can
send just the fields they know. Your code can handle this!
21
What does REST really do?
In the next chapter, we’ll dig into hypermedia and how it can
not only power the web, but your API as well. <<< :code:
../hacking-spring-boot-code :spring_boot_version: 2.3.0.M2
22
Getting the real message of REST out there
3
Getting the real message of REST
out there
In the previous chapter, I talked about how investing effort
in backward compatibility and having flexible settings, it’s
possible to avoid a lot of “API versioning”, something Roy
Fielding has decried. In this article, we’ll look more closely at
the depth and power of hypermedia.
23
Getting the real message of REST out there
{
"id": 1,
"name": "Gandalf",
"employees": [
"Frodo::ring bearer",
"Bilbo::burglar"
],
"_links": {
"self": {
"href": "http://localhost:8080/managers/1"
},
"managers": {
"href": "http://localhost:8080/managers"
},
"employees": {
"href": "http://localhost:8080/managers/1/employees"
}
}
}
24
Getting the real message of REST out there
old clients could access the data found there. But with a push
toward hypermedia consumption, they could navigate onto
the newer version of things.
@GetMapping(value = "/supervisors/{id}",
produces = MediaTypes.HAL_JSON_VALUE)
ResponseEntity<EntityModel<Supervisor>> findOne(
@PathVariable Long id) {
EntityModel<Manager> managerModel =
controller.findOne(id).getBody();
return ResponseEntity.ok(supervisorModel);
}
25
Getting the real message of REST out there
@Value
@JsonPropertyOrder({"id", "name", "employees"})
class Supervisor {
@JsonIgnore
private final Manager manager;
26
Getting the real message of REST out there
It’s why I’m giving you a sneak peek at something I’ve been
wanting to write for about three months. I find myself
attempting to explain the same concepts about REST on
Twitter, at meetups. In Berlin, I ran into a dear friend that
pinned me down and asked me hard questions on REST for
over an hour. Bring it on! That affirmed my gut decision to
create this:
27
Getting the real message of REST out there
compared to RPC-over-HTTP.
For a little something to chew on, I’ll close this article with
the following tweet.
28
Getting the real message of REST out there
Thanks,
Greg "Spring Boot hacker" Turnquist
29
Getting the real message of REST out there
30
About the Author
Greg L. Turnquist works on the Spring team as a principal
developer at VMware. He is a committer to Spring HATEOAS,
Spring Data, Spring Boot, R2DBC, and Spring Session for
MongoDB. He founded the Nashville JUG in 2010 and hasn’t
met a Java app (yet) that he doesn’t like.
Technical
Hacking with Spring Boot 2.3: Reactive Edition
Learning Spring Boot 2.0 2nd Edition
Learning Spring Boot [Video]
Learning Spring Boot
Python Testing Cookbook
Spring Python 1.1
Fiction
Darklight
The Job: A Darklight Chronicle
31