SlideShare a Scribd company logo
RESTful Data Services with
LinkRest
by Andrus Adamchik
@andrus_a
1
About Me
• Open source developer: ORM, DI, REST, no-container
• Member and ex-VP of the Apache Software Foundation
• Run a company called ObjectStyle
2
I Love REST
• HTTP is a clean and universal protocol
• Near-infinite scalability
3
Hi! I am a REST API Client
4
Domain Model
5
Domain Model Aggregates
6
Aggregate-Centric REST API
• An aggregate handled by a single REST resource
• Decoupling / encapsulation
• Assumes "normal" use cases don't cross aggregate boundaries
7
Problem #1: Too Much Data
curl -i 
'https://api.github.com/repos/nhl/link-rest/issues?state=all'
8
• Returns 126K JSON
• We need 3K ("title", "number", "id")
Problem #2: Too Little Data
9
curl -i 
'https://api.github.com/repos/nhl/link-rest/issues?state=all'
{
// O(N) problem:
// for each issue fetch events via a separate REST call
"events_url" : "https://api.github.com/.../issues/207/events"
...
}
LinkRest
• From aggregates to graphs
• Client decides on request shape / size
• One size fits all
• Reuse existing (ORM) models
10
Demo: Hockey Schedule App
11
LinkRest Protocol
12
13
Shaping Response
{"id":2016020001}
?include=id
{"id":2016020001,"homeTeam":{"name":"Rangers"}}
?include=id&include=homeTeam.name
?exclude=arena&exclude=dateTime
{"id":2016020001,"inProgress":false}
14
?cayenneExp=inProgress=true
SELECT t0.* FROM game t0 WHERE t0.in_progress = 1
SELECT t0.* FROM game t0 JOIN team t1 ON (t0.home_team_id = t1.id) WHERE
t1.name LIKE 'New York%'
?cayenneExp=["homeTeam.name like $t","New York%"]
Filtering
15
?sort=dateTime
SELECT t0.* FROM game t0 ORDER BY t0.date_time
SELECT t0.* FROM game t0 ORDER BY t0.date_time DESC
?sort=dateTime&dir=DESC
SELECT t0.* FROM game t0 JOIN team t1 ON (t0.home_team_id = t1.id) ORDER BY
t0.in_progress, t1.name DESC
?sort=[{"property":"inProgress"},{"property":"homeTeam.name","direction":"DESC"}]
Sorting
16
?start=2
{"data":[ /*objects 2..9*/ ], "total":10}
Pagination (for a list of 10 objects)
{"data":[ /*objects 0..4*/ ], "total":10}
?limit=5
{"data":[ /*objects 2..6*/ ], "total":10}
?start=2&limit=5
17
?include={"path":"homeGames","cayenneExp":"arena = 'Madison Square
Garden'","sort":"dateTime"}&include=homeGames.id
{
"homeGames": [
{ "id": 2016020075 },
{ "id": 2016020790 }
]
}
Advanced Include
Constraints API
18
• Prevents DOS attacks
• Fine-grained attribute security
ConstraintsBuilder<Team> tc = Constraint.idAndAttributes(Team.class);
ConstraintsBuilder<Game> gc = Constraint.idAndAttributes(Game.class)
.path("homeTeam", tc)
.path("visitingTeam", tc);
return LinkRest.select(Game.class, configuration)
.uri(uri)
.constraint(gc)
.get();
Demo: Modifying Data
19
Create/Update Flavors
• create(..)
• update(..)
• createOrUpdate(..)
• idempotentCreateOrUpdate(..)
• idempotentFullSync(..)
• delete(..)
20
Demo: Live Game Events App
21
Takeaway
• POJOs can be exposed via LinkRest using annotations
• Custom backend is plugged in as a "stage listener"
• "include" / "exclude" / "limit" supported automatically
• Other protocol features are up to the backend to implement
22
Demo: Hockey Schedule with Live Scores
23
Takeaway
• LinkRest can do parallel fetching and aggregation from multiple data
sources.
24
Great Minds Think Alike
25
Questions?
http://linkrest.io/
@andrus_a
26

More Related Content

LinkRest at JeeConf 2017