Recent Posts
Recent Comments
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- reactor core
- 웹 커리큘럼
- reactive
- reactor
- spring reactive
- ipTIME
- Spring Batch
- 공유기 서버
- 웹앱
- 서버운영
- 웹 스터디
- Spring Framework
Archives
- Today
- Total
Hello World
[Spring] HATEOAS 사용하기 본문
반응형
1. HATEOAS(Hypermedia As The Engine Of Application State)
HATEOAS는 RESTful API를 사용하는 클라이언트가 전적으로 서버에 의해 동적으로 상호작용을 할 수 있다. 쉽게 말하면 클라이언트가 서버에 요청시 서버는 요청에 의존되는 URI를 Response에 포함시켜 반환한다.
예를들면 사용자정보를 입력(POST)하는 요청 후 사용자를 조회(GET), 수정(PUT), 삭제(DELETE)할 수 있는 URI를 동적으로 알려주게 되는 것이다. 이렇게 동적으로 모든 요청에 의존되는 URI 정보를 보여준다면 아래와 같은 장점이 있을 것이다.
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"// } |
출처: http://blog.woniper.net/219
반응형
'Spring > Boot(4.x)' 카테고리의 다른 글
| [펌]Gradle 기반 Spring Boot 프로젝트 구축하기 (0) | 2016.02.16 |
|---|---|
| [펌]Spring Boot 에서 Java Config를 통해 myBatis 연동하기 (0) | 2016.01.22 |
| [펌]스프링 세션으로 스케일 아웃하기 Scaling out with Spring Session (0) | 2016.01.14 |
| Spring에서 RestTemplate을 사용하여 웹(API 서버)에 Multipart/Form-data (첨부파일 포함) 전송하기 (0) | 2016.01.10 |
| Spring에서 RestTemplate을 사용하여 REST 기반 서비스 요청과 테스트하기 (0) | 2016.01.10 |
Comments