๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒฟ Spring

[Spring Boot/RESTful] REST API 3๋‹จ๊ณ„๋ฅผ ์œ„ํ•œ HATEOAS ์„ค์ •

by nitronium102 2022. 2. 18.

HATEOAS

Hypermedia As the Engine Of Application State

ํ˜„์žฌ ๋ฆฌ์†Œ์Šค์™€ ์—ฐ๊ด€๋œ(ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•œ) ์ž์› ์ƒํƒœ ์ •๋ณด๋ฅผ ์ œ๊ณต(mapping)

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ช…์‹œ์ ์œผ๋กœ ๋งํฌ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ์„œ๋ฒ„ ์ธก์—์„œ ๋ฐ›์€ ๋งํฌ์˜ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ํ†ตํ•ด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ƒํƒœ ๋ฐ ์ „์ด๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์žฅ์ 

- ์š”์ฒญ URI๊ฐ€ ๋ณ€๊ฒฝ๋˜๋”๋ผ๋„ ํด๋ผ์ด์–ธํŠธ์—์„œ ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ URI๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ, ํด๋ผ์ด์–ธํŠธ๊ฐ€ URI ์ˆ˜์ •์— ๋”ฐ๋ฅธ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ํŽธ๋ฆฌํ•จ์„ ์ œ๊ณตํ•œ๋‹ค.
- URI ์ •๋ณด๋ฅผ ํ†ตํ•ด ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์„ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.
- Resource๊ฐ€ ํฌํ•จ๋œ URI๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ๋•Œ๋ฌธ์—, Resource์— ๋Œ€ํ•œ ํ™•์‹ ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

 

REST API ์„ค๊ณ„ ๋‹จ๊ณ„

0. The Swamp of POX

ํŠน์ •ํ•œ ์›น ํ”„๋กœํ† ์ฝœ(SOAP, XML-RPC)์„ ํ†ตํ•ด์„œ ์ปดํ“จํ„ฐ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ž์›์„ ์˜๋ฏธ ์—†์ด ์ „๋‹ฌ. POST ๋ฉ”์†Œ๋“œ๋งŒ ์‚ฌ์šฉํ•˜๋ฉฐ, ์„œ๋น„์Šค ๊ฐ„์— ๋‹จ์ผ POST ๋ฉ”์†Œ๋“œ๋กœ XML ๋ฐ์ดํ„ฐ๋ฅผ ๊ตํ™˜ํ•œ๋‹ค. 

1. Resources

ํ•จ์ˆ˜์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ธฐ๋Š” ๋Œ€์‹  REST URI๋ฅผ ์ด์šฉํ•œ๋‹ค. POST ๋ฉ”์†Œ๋“œ ํ•˜๋‚˜๋ฐ–์— ์‚ฌ์šฉํ•˜์ง€ ์•Š์ง€๋งŒ, POST ๋ฉ”์†Œ๋“œ๋กœ ์„œ๋น„์Šค๊ฐ„ ํ†ต์‹ ์„ ํ•˜๋ฉด์„œ ๋ณต์žกํ•œ ๊ธฐ๋Šฅ์„ ์—ฌ๋Ÿฌ ๋ฆฌ์†Œ์Šค๋กœ ๋ถ„์‚ฐ์‹œํ‚จ๋‹ค. 

2. HTTP Verbs

POST ์ด์™ธ์—๋„ GET, DELETE, PUT, HEAD ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. 

3. Hypermedia controls

์š”์ฒญ์— ๋Œ€ํ•œ Hypermedia ์‘๋‹ต ์†์— ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋‹ค์Œ์— ์ทจํ•ด์•ผ ํ•  ์•ก์…˜์— ๋Œ€ํ•œ ์ƒํƒœ ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋‹ค. ๋ฆฌ์†Œ์Šค์˜ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฆฌ์†Œ์Šค์™€ ํ•จ๊ป˜ ์ „๋‹ฌํ•œ๋‹ค. ํ•˜์ง€๋งŒ resource๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ถ€๊ฐ€์ ์ธ ์ •๋ณด๊นŒ์ง€ ๋‚˜ํƒ€๋‚ธ๋‹ค๋Š” ์ ์—์„œ ๋…ผ๋ž€์ด ์žˆ๋‹ค.

 

 

HATEOAS ์‚ฌ์šฉ

1. dependency ์ถ”๊ฐ€

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>

2. ๋ฒ„์ „๋ณ„ HATEOAS implementation ํ™•์ธ

pom.xml ์ œ์ผ ์ƒ๋‹จ์„ ๋ณด๋ฉด ์Šคํ”„๋ง๋ถ€ํŠธ ๋ฒ„์ „ ํ™•์ธ ๊ฐ€๋Šฅ
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.6.3</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

spring 2.1.8 RELEASE

  • Resource : ๋งŒ๋“ค๊ณ ์ž ํ•˜๋Š” ์ถ”๊ฐ€ ์ž์›์„ Resouce ๊ฐ์ฒด๋กœ ๋„ฃ์Œ
  • ControllerLinkBuilder : ๋งํฌํ•ด์คŒ
@GetMapping("/users/{id}")
public Resource<User> retrieveUser(@PathVariable int id){

	Resource<User> resource = new Resource<>(user);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass())
    				.retrieveAllUsers());
	resource.add(linkTo.withRel("all-users");

	return resource;
}

spring 2.2

  • EntityModel
  • WebMvcLinkBuilder : ์œ ์ œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜์‹œํ‚ฌ ๋•Œ ์œ ์ €๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ถ”๊ฐ€์ ์ธ ์ •๋ณด(๋งํฌ)๋ฅผ hypermedia ํ˜•์‹์œผ๋กœ ๋„ฃ์–ด๋†“๋Š”๋‹ค.
    • linkTo, methodOn ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” import ์‹œ static์„ ๋ถ™์—ฌ์ค˜์•ผ ํ•œ๋‹ค
    • linkTo() : ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” WebMvcLinkBuilder ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ 
    • methodOn(์—ฐ๊ฒฐ์‹œํ‚ฌ ๋ฉ”์†Œ๋“œ) : this.getClass()๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์ค‘์—์„œ retrieveAllUsers() ๋ฉ”์†Œ๋“œ๋ฅผ ์—ฐ๋™
@GetMapping("/users/{id}")
public Resource<User> retrieveUser(@PathVariable int id){

	// ๋งค๊ฐœ๋ณ€์ˆ˜์— ์œ ์ € ๊ฐ์ฒด๊ฐ’ ๋„ฃ์Œ
	EntityModel<User> entityModel = EntityModel.of(user);
	WebMvcLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllUsers());

	// retrieveAllUsers() ๋ฉ”์†Œ๋“œ๋ฅผ "all-users"๋กœ href ์—ฐ๊ฒฐ
    	// resource ๊ฐ์ฒด์— ๋งํฌ ์ถ”๊ฐ€
	entityModel.add(linkTo.withRel("all-users"));
        
	return model;
}

retrieveAllUsers()์™€ all-users ์—ฐ๋™ → /users์™€ all-users mapping

 

[์ฐธ๊ณ ]

HATEOAS ์„ค๋ช…

 

[Spring Boot] RESTful Service ๊ฐ•์˜ ์ •๋ฆฌ (6) - Implementing HATEOAS / level3๋‹จ๊ณ„์˜ REST API ๊ตฌํ˜„

[Spring Boot] RESTful Service ๊ฐ•์˜ ์ •๋ฆฌ (5) - REST API Version ๊ด€๋ฆฌ(URI, Request Parameter, Header, Mime Type ์ด์šฉ) [Spring Boot] RESTful Service ๊ฐ•์˜ ์ •๋ฆฌ (4) - Response ๋ฐ์ดํ„ฐ ์ œ์–ด๋ฅผ ์œ„ํ•œ Filterin..

prinha.tistory.com

๋ฆฌ์ฒ˜๋“œ์Šจ ์„ฑ์ˆ™ ๋ชจ๋ธ

๋Œ“๊ธ€