Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Spring HATEOAS
Search
Dai Yamasaki
November 15, 2019
Programming
0
1.1k
Spring HATEOAS
JSUG勉強会 2019その10 SpringOne Platform 2019報告会
Dai Yamasaki
November 15, 2019
Tweet
Share
Other Decks in Programming
See All in Programming
1から理解するWeb Push
dora1998
7
1.9k
Compose Multiplatform × AI で作る、次世代アプリ開発支援ツールの設計と実装
thagikura
0
170
AWS発のAIエディタKiroを使ってみた
iriikeita
1
190
Android 16 × Jetpack Composeで縦書きテキストエディタを作ろう / Vertical Text Editor with Compose on Android 16
cc4966
2
260
基礎から学ぶ大画面対応(Learning Large-Screen Support from the Ground Up)
tomoya0x00
0
3.2k
「待たせ上手」なスケルトンスクリーン、 そのUXの裏側
teamlab
PRO
0
560
複雑なフォームに立ち向かう Next.js の技術選定
macchiitaka
2
180
Deep Dive into Kotlin Flow
jmatsu
1
360
Putting The Genie in the Bottle - A Crash Course on running LLMs on Android
iurysza
0
140
概念モデル→論理モデルで気をつけていること
sunnyone
3
290
go test -json そして testing.T.Attr / Kyoto.go #63
utgwkk
3
310
Design Foundational Data Engineering Observability
sucitw
3
200
Featured
See All Featured
A better future with KSS
kneath
239
17k
Balancing Empowerment & Direction
lara
3
620
The World Runs on Bad Software
bkeepers
PRO
70
11k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
131
19k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
6k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
Why You Should Never Use an ORM
jnunemaker
PRO
59
9.5k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.1k
Designing for humans not robots
tammielis
253
25k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.1k
Transcript
4QSJOH)"5&0"4 %BJ:BNBTBLJ 5BHCBOHFST *OD
Who am I •Dai Yamasaki •Born to develop •I k6o
dislike Tokyo
r,BNFIBNFIB*** ΧΠέΞΦϦ l6B.BVLF&BPLBb⒖JOBJLB1POP ʢʮͷੜ໋ʢ㲈ओݖʣਖ਼ٛʹΑͬͯӬଓ͢Δʯʣz
Spring HATEOAS - Hypermedia APIs with Spring https://www.youtube.com/watch?v=o4cc6KzNrpI&list=PLAdzTan_eSPRlQ8t4TU5c-AB4SHV939M6&index=46
Who use HATEOAS
Migration script 0.x $ ./migrate-to-1.0.sh Migrating Spring HATEOAS references to
1.0 for files : *.java Adapting ./src/main/java/… … Done! https://github.com/spring-projects/spring-hateoas/tree/master/etc
Hypermedia -JOL -JOL -JOL -JOL
REST ख़ • Level 0 ҰͭͷURLͱҰͭͷHTTPϝιου • Level 1 ҟͳΔURLͱҰͭͷHTTPϝιου
• Level 2 ҟͳΔURLͱҟͳΔHTTPϝιου • Level 3 APIͷϨεϙϯεʹϋΠύʔϦϯΫใΛ࣋ͭ
Hypermedia + REST { "orderID":…, "productID”:…, "quantity":…, "orderValue":…, "_links":[ {
"rel":"customer", "href":"...", }, { "rel":"self", "href":"...", }] }
Hypermedia As The Engine Of Application State H A T
E O A S HATEOAS
Hypermedia + REST { "orderID":…, "productID”:…, "quantity":…, "orderValue":…, "_links":[ {
“rel":"cancel", "href":"...", }, { "rel":"self", "href":"...", }] }
Why HATEOAS • ϦϯΫ͕Θ͔Ε CRUD ͕Ͱ͖Δ • ϏδωεϩδοΫͷྲྀग़Λආ͚Δ ΫϥΠΞϯτϦϯΫͷ༗ແͰஅ •
ϖʔδϯάͷදݱʹ໎Θͳ͍ { "orderID":…, "productID”:…, "quantity":…, "orderValue":…, "_links":[ { “rel":"cancel", "href":"...", }, { "rel":"self", "href":"...", }] }
Spring HATEOAS
Representation Model
Representation Model
Links & Affordance
ΞϑΥʔμϯεʢaffordanceʣͱɺ ڥ͕ಈʹରͯ͠༩͑Δʮҙຯʯͷ͜ͱͰ͋Δɻ ΞϝϦΧͷ֮৺ཧֶऀδΣʔϜζɾJɾΪϒιϯʹΑΔޠͰ͋Γɺ ੜଶޫֶɺੜଶ৺ཧֶͷجఈత֓೦Ͱ͋Δɻ ʮ༩͑Δɺఏڙ͢Δʯͱ͍͏ҙຯͷӳޠ afford ͔ΒΒΕͨɻ Affordances
Affordance https://www.amazon.co.jp/%E3%82%B5%E3%83%B3%E3%82%A2%E3%83%BC%E3%83%88-%E3%81%8A%E3%82%82%E3%81%97%E3%82%8D%E9%A3%9F%E5%99%A8-4%E6%96%87%E5%AD%97%E3%83%9D%E3%83%AA%E3%82%B7%E3%83%BC- %E3%83%9E%E3%82%B0%E3%82%AB%E3%83%83%E3%83%97-SAN1858/dp/B003NEPOTW
Links & Affordances { "name": "Dai Yamasaki", "role": "Developer", "_links":
{ "self": { "href": “/employees/0" }, "employees": { "href": "/employees" } }, "_templates": { "default": { "method": "put", "properties": [ { "name": "name", "required": true }, { "name": "role" } ] }, "partiallyUpdateEmployee": { "method": "patch", "properties": [ { "name": "name" }, { "name": "role" } …
Links & Affordances @GetMapping("/employees/{id}") public EntityModel<Employee> findOne(@PathVariable Integer id) {
Employee employee = employeeRepository.findOneById(id); // ࣗࣗ "self" ͷϦϯΫΛ࡞ WebMvcEmployeeController controller = methodOn(WebMvcEmployeeController.class); Link findOneLink = linkTo(controller.findOne(id)).withSelfRel(); // Affordance ΛؚΊͨใΛฦ͢ return new EntityModel<>( employee, findOneLink .andAffordance( afford(controller.updateEmployee(null, id))) .andAffordance( afford(controller.partiallyUpdateEmployee(null, id)))); }
4FQBSBUFSFTPVSDF Links & Affordances { "_links": { "my-app:payment": {...}, "curies":
[ { "name": "my-app", "href": "http://hoge.com/docs/{rel}", "templated": true }] } } IUUQNZBQQDPNEPDTQBZNFOU
*OMJOFSFTPVSDF { ... "_templates": { "default": { "method": "put", "properties":
[{ "name": "number", "regex": "[0-9]{16}" }] } } } Links & Affordances
.FEJB5ZQFT
5SBWFSTPO @Test void traverson() { Traverson traverson = new Traverson(baseUri,
MediaTypes.HAL_JSON); ParameterizedTypeReference<EntityModel<Actor>> typeReference = new ParameterizedTypeReference<EntityModel<Actor>>() {}; EntityModel<Actor> result = traverson.follow("movies", "movie", “actor") .toObject(typeReference); assertThat(result.getContent().name) .isEqualTo("Dwayne Douglas Johnson"); }
5SBWFSTPO { "_links": { "movie": { "href": "http://localhost:8080/movies/63e0c973-1606-4fe7-b4e3-0de03d2e0c06" } }
} { "title": "Fast & Furious Presents: Hobbs & Shaw", "_links": { "actor": { "href": "http://localhost:8080/actors/f58ebb17-abd8-48fb-b06c-5a912251631c" } } } { "name": "Dwayne Douglas Johnson" } NPWJFT EntityModel<Actor> result = traverson.follow("movies", "movie", “actor") .toObject(typeReference); NPWJF BDUPS
0UIFS • RepresentationModelAssembler • I18N support • LinkRelationProvider
5IBOLT