1. HATEOAS(Hypermedia As The Engine Of Application State)
HATEOAS는 RESTful API를 사용하는 클라이언트가 전적으로 서버에 의해 동적으로 상호작용을 할 수 있다. 쉽게 말하면 클라이언트가 서버에 요청시 서버는 요청에 의존되는 URI를 Response에 포함시켜 반환한다.
예를들면 사용자정보를 입력(POST)하는 요청 후 사용자를 조회(GET), 수정(PUT), 삭제(DELETE)할 수 있는 URI를 동적으로 알려주게 되는 것이다. 이렇게 동적으로 모든 요청에 의존되는 URI 정보를 보여준다면 아래와 같은 장점이 있을 것이다.
- 요청 URI정보가 변경되어도 클라이언트에서 동적으로 생성된 URI를 사용한다면, 클라이언트 입장에서는 URI 수정에 따른 코드 변경이 불필요하다.
- URI정보를 통해 의존되는 요청을 예측가능하게 한다.
- 기본 URI정보가 아니라 resource까지 포함된 URI를 보여주기 때문에 resource에 대한 확신을 갖게된다.
RESTful에 대한 설명은 생략되어 있기 때문에 RESTful에 대한 이해가 없다면 위 내용이 이해가 안될수 있다.
2. Spring HATEOAS
Spring에서는 HATEOAS를 사용하기 쉽게 이미 만들어놨다.
Spring에서 제공하는 HATEOAS 템플릿 프로젝트
3. 예제
Maven
1 2 3 4 5 | <dependency> <groupid>org.springframework.hateoas</groupid> <artifactid>spring-hateoas</artifactid> <version>0.15.0.RELEASE</version></dependency> |
Resources
1 2 3 4 5 | class PersonResource extends ResourceSupport { int id; String firstname; String lastname;} |
HATEOAS를 사용하기 위해서는 DTO객체에 org.springframework.hateoas.ResourceSupport 클래스를 상속받아야한다.
Resources에 link 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | PersonResource resource = new PersonResource();resource.id = 1;resource.firstname = "Lee";resource.lastname = "Woniper";// 결과//{// id:1,// firstname : "Lee",// lastname : "Woniper",// links : [// { rel : "self",// href : "http://localhost:8080/person" }// ]//} |
ResourceSupport클래스를 상속받아야만 Link를 add할 수 있다.
1 2 3 4 5 6 | @Controller@RequestMapping(value="/hateoas")public class HATEOASController { ... ...} |
Link 생성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | PersonResource resource = new PersonResource();resource.id = 1;resource.firstname = "Lee";resource.lastname = "Woniper";// 1. 기본 URILink link = linkTo(HATEOASController.class).withRel("manager");resource.add(link);// 결과//{// id:1,// firstname : "Lee",// lastname : "Woniper",// links : [// { rel : "manager",// href : "http://localhost:8080/hateoas" }// ]//}// 2. Resource 추가 URILink resourceLink = linkTo(HATEOASController.class).slash(person.id).withRel("manager");resource.add(resourceLink);// 결과//{// id:1,// firstname : "Lee",// lastname : "Woniper",// links : [// { rel : "manager",// href : "http://localhost:8080/hateoas/1" }// ]//}// 3. URI ListList<link> list = new ArrayList<>();resource.add(list);// 결과// id:1,// firstname : "Lee",// lastname : "Woniper",// links: [// 0: { rel: "self",// href: "http://localhost:8080/test1"// }// 1: { rel: "self"// href: "http://localhost:8080/test2"// }// 2: { rel: "self"// href: "http://localhost:8080/test3"// }// ]// 4. Method URI// Method에 URI를 직접선택할 수 있다.Link methodLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(HATEOASController.class).getPerson(2L)).withSelfRel();resource.add(methodLink);// 결과// id:1,// firstname : "Lee",// lastname : "Woniper",// links: [// { rel: "self",// href: "http://localhost:8080/hateoas/person/2"// } |
출처 : https://blog.woniper.net/219?category=699184
'Spring Framework > Spring Core' 카테고리의 다른 글
| lombok (롬복의 특징, annotations) (0) | 2021.03.22 |
|---|---|
| EHCache (ehcache-spring-annotations, ehcache.xml, @Cacheable) (0) | 2021.03.22 |
| 스프링프레임워크로 카카오 챗봇 만들기 (0) | 2021.03.22 |
| HandlerMethodArgumentResolverComposite Class Composite 패턴 (0) | 2020.09.03 |
| 이메일 전송 (0) | 2020.09.01 |
| XML 스키마 기반의 POJO 클래스를 이용한 AOP 구현 (0) | 2020.07.06 |
| [Spring] AOP 개요 (0) | 2020.07.06 |
| [Spring] Locale 처리 (0) | 2020.07.06 |