chap 7 jihoonKim - JAVA-JIKIMI/SPRING-IN-ACTION-5 GitHub Wiki
- RestTemplate์ ์ฌ์ฉํด์ REST API ์ฌ์ฉํ๊ธฐ
- Traverson์ ์ฌ์ฉํด์ ํ์ดํผ๋ฏธ๋์ด API ์ด๋ํ๊ธฐ
-
RestTemplate์ REST ๋ฆฌ์์ค๋ฅผ ์ฌ์ฉํ๋ ๋ฐ ๋ฒ์กํ ์ผ์ ์ฒ๋ฆฌํด์ค๋ค.
-
REST ๋ฆฌ์์ค์ ์ํธ ์์ฉํ๊ธฐ ์ํ 41๊ฐ ๋ฉ์๋ ์ ๊ณต
๋ฉ์๋ ๊ธฐ๋ฅ ์ค๋ช delete(..) ์ง์ ๋ URL์ ๋ฆฌ์์ค์ HTTP DELETE ์์ฒญ์ ์ํํ๋ค. exchange(..) ์ง์ ๋ HTTP ๋ฉ์๋๋ฅผ URL์ ๋ํด ์คํํ๋ฉฐ, Response body์ ์ฐ๊ฒฐ๋๋ ๊ฐ์ฒด๋ฅผ ํฌํจํ๋ responseEntity๋ฅผ ๋ฐํํ๋ค. execute(..) ์ง์ ๋ HTTP ๋ฉ์๋๋ฅผ URL์ ๋ํด ์คํํ๋ฉฐ, Response body์ ์ฐ๊ฒฐ๋๋ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค. getForEntity(..) HTTP GET ์์ฒญ์ ์ ์กํ๋ฉฐ, Response body์ ์ฐ๊ฒฐ๋๋ ๊ฐ์ฒด๋ฅผ ํฌํจํ๋ ResponseEntity๋ฅผ ๋ฐํํ๋ค. getForObject(..) HTTP GET ์์ฒญ์ ์ ์กํ๋ฉฐ, Response body์ ์ฐ๊ฒฐ๋๋ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค. headForHeaders(..) HTTP HEAD ์์ฒญ์ ์ ์กํ๋ฉฐ, ์ง์ ๋ ๋ฆฌ์์ค URL์ HTTP ํค๋๋ฅผ ๋ฐํํ๋ค. optionsForAllow(..) HTTP OPTIONS ์์ฒญ์ ์ ์กํ๋ฉฐ, ์ง์ ๋ URL์ Allow ํค๋๋ฅผ ๋ฐํํ๋ค. patchForObject(..) HTTP PATCH ์์ฒญ์ ์ ์กํ๋ฉฐ, Response body์ ์ฐ๊ฒฐ๋๋ ๊ฒฐ๊ณผ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค. postForEntity(..) URL์ ๋ฐ์ดํฐ๋ฅผ POSTํ๋ฉฐ, Resonse body์ ์ฐ๊ฒฐ๋๋ ๊ฐ์ฒด๋ฅผ ํฌํจํ๋ ResponseEntity๋ฅผ ๋ฐํํ๋ค. postForLocation(..) URL์ ๋ฐ์ดํฐ๋ฅผ POSTํ๋ฉฐ, ์๋ก ์์ฑ๋ ๋ฆฌ์์ค์ URL๋ฅผ ๋ฐํํ๋ค. postForObject(..) URL์ ๋ฐ์ดํฐ๋ฅผ POSTํ๋ฉฐ, Response body์ ์ฐ๊ฒฐ๋๋ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค. put(..) ๋ฆฌ์์ค ๋ฐ์ดํฐ๋ฅผ ์ง์ ๋ URL์ PUTํ๋ค. - ๊ณ ์ ํ ์์ ์ ์ํํ๋ ๋ฉ์๋๋ 12๊ฐ์ด๊ณ , ๋๋จธ์ง๋ ์ด ๋ฉ์๋๋ค์ ์ค๋ฒ๋ก๋ฉ ๋ฒ์
- TRACE๋ฅผ ์ ์ธํ ํ์ค HTTP ๋ฉ์๋ ๊ฐ๊ฐ์ ๋ํด ์ต์ํ ํ๋์ ๋ฉ์๋๋ฅผ ๊ฐ๊ณ ์๋ค.
- execute()์ exchange()๋ ๋ชจ๋ HTTP ๋ฉ์๋ ์์ฒญ์ ์ ์กํ๊ธฐ ์ํ ์ ์์ค ๋ฒ์ฉ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
- ์ ํ์ ๋ฉ์๋๋ ์ธ ๊ฐ์ง ํํ๋ก ์ค๋ฒ๋ก๋ฉ๋์ด ์๋ค.
- ๊ฐ๋ณ ์ธ์ ๋ฆฌ์คํธ์ ์ง์ ๋ URL ๋งค๊ฐ๋ณ์์ URL ๋ฌธ์์ด(String ํ์ )์ ์ธ์๋ก ๋ฐ๋๋ค.
- Map<String, String>์ ์ง์ ๋ URL ๋งค๊ฐ๋ณ์์ URL ๋ฌธ์์ด์ ์ธ์๋ก ๋ฐ๋๋ค.
- java.net.URI๋ฅผ URL์ ๋ํ ์ธ์๋ก ๋ฐ์ผ๋ฉฐ, ๋งค๊ฐ๋ณ์ํ๋ URL์ ์ง์ํ์ง ์๋๋ค.
-
RestTemplate์ ์ฌ์ฉํ๊ธฐ ์ํด ํ์ ์์ ์ RestTemplate ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ฑฐ๋, ๋น์ผ๋ก ์ ์ธํ๊ณ ํ์ ์ ์ฃผ์ ํ๋ค.
RestTemplate rest = new RestTemplate();
@Bean public RestTemplate restTemplate() { return new RestTemplate(); }
๋ค์ ์ฝ๋๋ RestTemplate์ ์ฌ์ฉํด์ ํน์ ID๋ฅผ ๊ฐ๋ Ingredient ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์จ๋ค.
HATEOAS๊ฐ ํ์ฑํ๋์ง ์์๋ค๋ฉด getForObject()๋ก ์์์ฌ(ingredient)๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค.
public Ingredient getIngredientById(String ingredientId) {
return rest.getForObject("http://localhost:8080/ingredients/{id}",
Ingredient.class, ingredientId);
}
- getForObject()์ ์ ๋ฌ๋ ingredientId ๋งค๊ฐ๋ณ์๋ ์ง์ ๋ URL์ {id} ํ๋ ์ด์ค ํ๋์ ์ฌ์ฉ๋๋ค.
- ์ด ์์๋ ํ๋์ ๋ณ์๋ง ์์ง๋ง, ๋ณ์ ๋งค๊ฐ๋ณ์๋ค์ ์ฃผ์ด์ง ์์๋๋ก ํ๋ ์ด์คํ๋์ ์ง์ ๋๋ค.
- getForObject()์ 2 ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ ์๋ต์ด ๋ฐ์ธ๋ฉ๋๋ ํ์ ์ด๋ค. ์ฌ๊ธฐ์๋ JSON ํ์์ธ ์๋ต ๋ฐ์ดํฐ๊ฐ ๊ฐ์ฒด๋ก ์ญ์ง๋ ฌํ๋์ด ๋ฐํ๋๋ค.
Map์ ์ฌ์ฉํด์ URL ๋ณ์๋ค์ ์ง์ ํ ์ ์๋ค.
public Ingredient getIngredientById(String ingredientId) {
Map<String, String> urlVariables = new HashMap<>();
urlVariables.put("id", ingredientId);
return rest.getForObject("http://localhost:8080/ingredients/{id}",
Ingredient.class, urlVariables);
}
- ์์ฒญ์ด ์ํ๋ ๋ {id} ํ๋ ์ด์คํ๋๋ ํค๊ฐ id์ธ Map ํญ๋ชฉ ๊ฐ(ingredientId ๊ฐ)์ผ๋ก ๊ต์ฒด๋๋ค.
URI ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ ๋๋ URI ๊ฐ์ฒด๋ฅผ ๊ตฌ์ฑํ์ฌ getForObject()๋ฅผ ํธ์ถํด์ผ ํ๋ค.
public Ingredient getIngredientById(String ingredientId) {
Map<String, String> urlVariables = new HashMap<>();
urlVariables.put("id", ingredientId);
URI url = UriComponentsBuilder
.fromHttpUrl("http://localhost:8080/ingredients/{id}")
.build(urlVariables);
return rest.getForObject(url, Ingredient.class);
}
- URI ๊ฐ์ฒด๋ URL ๋ฌธ์์ด ๋ช ์ธ๋ก ์์ฑ๋๋ฉฐ, ์ด ๋ฌธ์์ด์ {id} ํ๋ ์ด์คํ๋๋ Map ํญ๋ชฉ ๊ฐ์ผ๋ก ๊ต์ฒด๋๋ค.
- getForObject() ๋ฉ์๋๋ ๋ฆฌ์์ค๋ก ๋๋ฉ์ธ ๊ฐ์ฒด๋ง ๊ฐ์ ธ์์ ์๋ต ๊ฒฐ๊ณผ๋ก ๋ฐํํ๋ค.
- ํด๋ผ์ด์ธํธ๊ฐ ์ด์ธ์ ์ถ๊ฐ๋ก ํ์ํ ๊ฒ์ด ์๋ค๋ฉด getForEntity๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
getForEntity()๋ getForObject()์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ๋์ํ์ง๋ง, ์๋ต ๊ฒฐ๊ณผ๋ฅผ ๋ํ๋ด๋ ๋๋ฉ์ธ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ ๋์ ๋๋ฉ์ธ ๊ฐ์ฒด๋ฅผ ํฌํจํ๋ ResponseEntity ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
ResponseEntity์๋ Response ํค๋์ ๊ฐ์ ๋ ์์ธํ ์๋ต ์ฝํ
์ธ ๊ฐ ํฌํจ๋ ์ ์๋ค.
public Ingredient getIngredientById(String ingredientId) {
ResponseEntity<Ingredient> responseEntity =
rest.getForEntity("http://localhost:8080/ingredients/{id}",
Ingredient.class, ingredientId);
log.info("Fetched time: " +
responseEntity.getHeaders().getDate());
return responseEntity.getBody();
}
- getForEntity() ๋ฉ์๋๋ getForObject()์ ๋์ผํ ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ๋๋ก ์ค๋ฒ๋ก๋ฉ๋์ด ์๋ค.
๋ฐ๋ผ์ URL ๋ณ์๋ค์ ๊ฐ๋ณ ์ธ์ ๋ฆฌ์คํธ๋ URI ๊ฐ์ฒด๋ก ์ ๋ฌํ์ฌ getForEntity()๋ฅผ ํธ์ถํ ์ ์๋ค.
๋ค์ ์ฝ๋๋ RestTemplate์ ์ฌ์ฉํด์ ํน์ ID๋ฅผ ๊ฐ๋ Ingredient ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์จ๋ค. put() ๋ฉ์๋๋ 3๊ฐ์ ์ค๋ฒ๋ก๋ฉ๋ ๋ฒ์ ์ด ์์ผ๋ฉฐ, ์ง๋ ฌํ๋ ํ ์ง์ ๋ URL๋ก ์ ์ก๋๋ Object ํ์ ์ ์ธ์๋ก ๋ฐ๋๋ค.
// ํน์ ์์์ฌ ๋ฆฌ์์ค๋ฅผ ์๋ก์ด Ingredient ๊ฐ์ฒด์ ๋ฐ์ดํฐ๋ก ๊ต์ฒด
public void updateIngredientById(Ingredient ingredientId) {
rest.put("http://localhost:8080/ingredients/{id}",
ingredient,
ingredient.getId());
}
- ์ฌ๊ธฐ์ URL์ ๋ฌธ์์ด๋ก ์ง์ ๋์๊ณ ์ธ์๋ก ์ ๋ฌ๋๋ Ingredient ๊ฐ์ฒด์ id ์์ฑ ๊ฐ์ผ๋ก ๊ต์ฒด๋๋ ํ๋ ์ด์คํ๋๋ฅผ ๊ฐ๋๋ค.
- put() ๋ฉ์๋๋ Ingredient ๊ฐ์ฒด ์์ฒด๋ฅผ ์ ใ ๋ํ๋ฉฐ, ๋ฐํ ํ์ ์ void๋ผ์ ๋ฐํ๊ฐ ์ฒ๋ฆฌํ ํ์๋ ์๋ค.
// ํน์ ์์์ฌ ์ญ์
public void deleteIngredient(Ingredient ingredientId) {
rest.delete("http://localhost:8080/ingredients/{id}",
ingredient.getId());
}
- ๋ฌธ์์ด๋ก ์ง์ ๋ URL๊ณผ URL ๋ณ์ ๊ฐ๋ง delete()์ ์ธ์๋ก ์ ๋ฌํ๋ค.
- ๋ค๋ฅธ ๋ฉ์๋์ ๋ง์ฐฌ๊ฐ์ง๋ก, URL์ Map์ผ๋ก ๋ URL ๋งค๊ฐ๋ณ์๋ URL ๊ฐ์ฒด๋ก ์ง์ ๋ ์ ์๋ค.
public Ingredient createIngredient(Ingredient ingredientId) {
return rest.postForObject("http://localhost:8080/ingredients/{id}",
ingredient, Ingredient.class);
}
- POST ์์ฒญ์ด ์ํ๋ ํ ์๋ก ์์ฑ๋ Ingredient ๋ฆฌ์์ค๋ฅผ ๋ฐํ๋ฐ์ ์ ์๋ค.
- ๋ฌธ์์ด URL๊ณผ ์๋ฒ์ ์ ์ก๋ ๊ฐ์ฒด ๋ฐ ์ด ๊ฐ์ฒด์ ํ์ (๋ฆฌ์์ค body์ ๋ฐ์ดํฐ์ ์ฐ๊ด๋)์ ์ด์๋ก ๋ฐ๋๋ค.
- URL ๋ณ์๊ฐ์ ๊ฐ๋ Map์ด๋ URL์ ๋์ฒดํ ๊ฐ๋ณ ๋งค๊ฐ๋ณ์ ๋ฆฌ์คํธ๋ฅผ ๋ค ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ ๊ฐ๋ฅํ๋ค.
public URI createIngredient(Ingredient ingredient) {
return rest.postForLocation("http://localhost:8080/ingredients",
ingredient, Ingredient.class);
- postForObject()์ ๋์ผํ๊ฒ ์๋ํ์ง๋ง, ๋ฆฌ์์ค ๊ฐ์ฒด ๋์ ์๋ก ์์ฑ๋ ๋ฆฌ์์ค์ URI๋ฅผ ๋ฐํ ๋ฐ๋๋ค.
- ๋ฐํ๋ URI๋ ํด๋น Response์ Location ํค๋์์ ์ป๋๋ค.
public Ingredient createIngredient(Ingredient ingredient) {
ResponseEntity<Ingredient> responseEntity =
rest.postForEntity("http://localhost:8080/ingredients",
ingredient, Ingredient.class);
log.info("New resource created at " +
responseEntity.getHeaders().getLocation());
return responseEntity.getBody();
}
- ๋ฆฌ์์ค ๊ฐ์ฒด์ ์๋ก ์์ฑ๋ ๋ฆฌ์์ค์ URI ๋ชจ๋ ํ์ํ ๋ ์ฌ์ฉํ ์ ์๋ค.
API์์ ํ์ดํผ๋งํฌ๋ฅผ ํฌํจํด์ผ ํ๋ค๋ฉด RestTempate์ ๋์์ด ์ ๋๋ค.
(๋ ์์ธํ ๋ฆฌ์์ค ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ ๊ทธ ์์ ํฌํจ๋ ์ฝํ
์ธ ์ ๋งํฌ๋ฅผ ์ฌ์ฉํ ์ ๋ ์์ง๋ง, ๊ฐ๋จํ์ง๋ ์๋ค)
Traverson์ ์คํ๋ง ๋ฐ์ดํฐ HATEOAS์ ๊ฐ์ด ์ ๊ณต๋๋ฉฐ, ์คํ๋ง ์ ํ๋ฆฌ์ผ์ด์ ์์ ํ์ดํผ ๋ฏธ๋์ด API๋ฅผ ์ฌ์ฉํ ์ ์๋ ์๋ฃจ์ ์ด๋ค.
- ์๋ฐ ๊ธฐ๋ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- '๋์๋ค๋๋ค(Traverse on)'์ ์๋ฏธ๋ก, ์ฌ๊ธฐ์๋ ๊ด๊ณ ์ด๋ฆ์ผ๋ก ์ํ๋ API๋ฅผ (์ด๋ํ๋ฉฐ) ์ฌ์ฉํ ๊ฒ์ด๋ค.
Traversion์ ์ฌ์ฉํ ๋๋ ์ฐ์ ํด๋น API์ ๊ธฐ๋ณธ URI๋ฅผ ๊ฐ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ผ ํ๋ค.
Traverson traverson = new Traverson(
URI.create("http://localhost:8080/api"), MediaTypes.HAL_JSON);
- Traverson์๋ URL๋ง ์ง์ ํ๋ฉด ๋๊ณ , ์ดํ๋ถํฐ๋ ๊ฐ ๋งํฌ์ ๊ด๊ณ ์ด๋ฆ์ผ๋ก API๋ฅผ ์ฌ์ฉํ๋ค.
- Traverson ์์ฑ์์๋ ํด๋น API๊ฐ HAL ์คํ์ผ์ ํ์ดํผ๋งํฌ๋ฅผ ๊ฐ๋ JSON ์๋ต์ ์์ฑํ๋ค๋ ๊ฒ์ ์ธ์๋ก ์ง์ ํ ์ ์๋ค.
- ์ด ์ธ์๋ฅผ ์ง์ ํ๋ ์ด์ ๋ ์์ ๋๋ ๋ฆฌ์์ค ๋ฐ์ดํฐ๋ฅผ ๋ถ์ํ๋ ๋ฐฉ๋ฒ์ Traverson์ด ์ ์ ์๊ฒ ํ๊ธฐ ์ํจ์ด๋ค.
- ์ด๋์๋ Traverson์ด ํ์ํ ๋ Traverson ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ฑฐ๋, ์ฃผ์ ๋๋ ๋น์ผ๋ก ์ ์ธํ ์ ์๋ค.
// ๋ชจ๋ ์์์ฌ ๋ฆฌ์คํธ ๊ฐ์ ธ์ค๊ธฐ
public Iterable<Ingredient> getAllIngredientsWithTraverson() {
ParameterizedTypeReference<Resources<Ingredient>> ingredientType =
new ParameterizedTypeReference<Resources<Ingredient>>() {};
Resources<Ingredient> ingredientRes = traverson
.follow("ingredients")
.toObject(ingredientType);
Collection<Ingredient> ingredients = ingredientRes.getContent();
return ingredients;
}
- ๊ฐ ingredients ๋งํฌ๋ค์ ํด๋น ์์์ฌ ๋ฆฌ์์ค๋ฅผ ๋งํฌํ๋ href ์์ฑ์ ๊ฐ์ง๋ฏ๋ก ๊ทธ ๋งํฌ๋ฅผ ๋ฐ๋ผ๊ฐ๋ฉด ๋๋ค.
- follow() ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ๋ฆฌ์์ค ๋งํฌ์ ๊ด๊ณ ์ด๋ฆ์ด ingredients์ธ ๋ฆฌ์์ค๋ก ์ด๋ํ ์ ์๋ค.
- ์ด ์์ ์์ ํด๋ผ์ด์ธํธ๋ ingredients๋ก ์ด๋ํ์ผ๋ฏ๋ก toObject()๋ฅผ ํธ์ถํ์ฌ ํด๋น ๋ฆฌ์์ค์ ์ฝํ ์ธ ๋ฅผ ๊ฐ์ ธ์์ผ ํ๋ค.
- toObject() ์ธ์์๋ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด ๋ค์ด๋ ๊ฐ์ฒด์ ํ์
์ ์ง์ ํด์ผ ํ๋ค.
- Resources ํ์ ์ ๊ฐ์ฒด๋ก ์ฝ์ด ๋ค์ฌ์ผ ํ๋๋ฐ, ์๋ฐ์์๋ ๋ฐํ์ ์์ ์ ๋ค๋ฆญ ํ์ ์ ํ์ ์ ๋ณด()๊ฐ ์๊ฑฐ๋์ด ๋ฆฌ์์ค ํ์ ์ ์ง์ ํ๊ธฐ ์ด๋ ต๋ค.
- ParameterizedTypeReference๋ฅผ ์์ฑํ๋ฉด ๋ฆฌ์์ค ํ์ ์ ์ง์ ํ ์ ์๋ค.
// ๊ฐ์ฅ ์ต๊ทผ์ ์์ฑ๋ ํ์ฝ๋ค ๊ฐ์ ธ์ค๊ธฐ
public Iterable<Taco> getRecentTacosWithTraverson() {
ParameterizedTypeReference<Resources<Taco>> tacoType =
new ParameterizedTypeReference<Resources<Taco>>() {};
Resources<Taco> tacoRes = traverson
.follow("tacos")
.follow("recents")
.toObject(tacoType);
// Alternatively, list the two paths in the same call to follow()
// Resources<Taco> tacoRes = traverson
// .follow("tacos", "recents")
// .toObject(tacoType);
return tacoRes.getContent();
}
- tacos ๋งํฌ ๋ค์ recents ๋งํฌ๋ฅผ ๋ฐ๋ผ๊ฐ๋ค.
Traverson์ ์ฌ์ฉํ๋ฉด HATEOAS๊ฐ ํ์ฑํ๋ API๋ฅผ ์ด๋ํ๋ฉด์ ํด๋น API์ ๋ฆฌ์์ค๋ฅผ ์ฝ๊ฒ ๊ฐ์ ธ์ฌ ์ ์๋ค.
Traverson์ API์ ๋ฆฌ์์ค๋ฅผ ์ฐ๊ฑฐ๋ ์ญ์ ํ๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํ์ง ์๋๋ค(RestTemplate์ ๋ฆฌ์์ค๋ฅผ ์ฐ๊ฑฐ๋ ์ญ์ ํ ์ ์์ง๋ง, API ์ด๋์ด ์ด๋ ต๋ค).
API์ ์ด๋๊ณผ ๋ฆฌ์์ค์ ๋ณ๊ฒฝ ๋๋ ์ญ์ ๋ชจ๋๋ฅผ ํด์ผ ํ๋ค๋ฉด RestTemplate๊ณผ Traverson์ ํจ๊ป ์ฌ์ฉํด์ผ ํ๋ค.
Traverson์ ์๋ก์ด ๋ฆฌ์์ค๊ฐ ์์ฑ๋ ๋งํฌ๋ก ์ด๋ํ ๋๋ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ์ด๋ํ ๋ค์ ํด๋น ๋งํฌ๋ฅผ RestTemplate์ ์ง์ ํ์ฌ HTTP ์์ฒญ์ด ๊ฐ๋ฅํ๋ค.
// ์๋ก์ด ์์์ฌ(Ingredient ๊ฐ์ฒด) ์ถ๊ฐ
public Ingredient addIngredient(Ingredient ingredient) {
String ingredientsUrl = traverson
.follow("ingredients")
.asLink()
.getHref();
return rest.postForObject(ingredientsUrl,
ingredient,
Ingredient.class);
}
- ingredients ๋งํฌ๋ฅผ ๋ฐ๋ผ๊ฐ ํ์ asLink()๋ฅผ ํธ์ถํ์ฌ ingredients ๋งํฌ ์์ฒด๋ฅผ ์์ฒญํ๋ค.
- getHref()๋ฅผ ํธ์ถํ์ฌ ์ด ๋งํฌ์ URL์ ๊ฐ์ ธ์จ๋ค.
- ๊ฐ์ ธ์จ URL์ ๋งค๊ฐ๋ณ์๋ก ํ์ฌ RestTempate ์ธ์คํด์ค์ postForObject()๋ฅผ ํธ์ถ ๋ฐ ์๋ก์ด ์์์ฌ๋ฅผ ์ถ๊ฐํ ์ ์๋ค.
- ํด๋ผ์ด์ธํธ๋ RestTemplate์ ์ฌ์ฉํด์ REST API์ ๋ํ HTTP ์์ฒญ์ ํ ์ ์๋ค.
- Traverson์ ์ฌ์ฉํ๋ฉด ํด๋ผ์ด์ธํธ๊ฐ ์๋ต์ ํฌํจ๋ ํ์ดํผ๋งํฌ๋ฅผ ์ฌ์ฉํด์ ์ํ๋ API๋ก ์ด๋ํ ์ ์๋ค.