Notice
Recent Posts
Recent Comments
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 |
Tags
- REST API
- Spring
- IntelliJ
- batch
- spring boot tomcat
- spring-webmvc #코드읽기
- tomcat
- spring batch 코드
- spring jpa
- docker
- Spring Data JPA
- spring camp
- spring bean
- JPA
- JUnit
- Spring Batch
- 톰캣
- ApplicationPidFileWriter
- static inner class
- spring-webmvc
- SuperTypeToken
- spring pid
- Spring Data REST
- spring boot
- 코드 리뷰
- JPA mapping
- 세미나
- spring-mvc
- Data REST
- ORM
Archives
- Today
- Total
woniper
HATEOAS 사용하기 본문
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
org.springframework.hateoas spring-hateoas 0.15.0.RELEASE
Resources
class PersonResource extends ResourceSupport { int id; String firstname; String lastname; }
HATEOAS를 사용하기 위해서는 DTO객체에 org.springframework.hateoas.ResourceSupport 클래스를 상속받아야한다.
Resources에 link 추가
PersonResource resource = new PersonResource(); resource.id = 1; resource.firstname = "Lee"; resource.lastname = "Woniper"; resource.add(new Link("http://localhost:8080/person")); // 결과 //{ // id:1, // firstname : "Lee", // lastname : "Woniper", // links : [ // { rel : "self", // href : "http://localhost:8080/person" } // ] //}ResourceSupport클래스를 상속받아야만 Link를 add할 수 있다.
@Controller @RequestMapping(value="/hateoas") public class HATEOASController { ... ... }위와같은 Controller가 존재한다고 가정하자.
Link 생성
PersonResource resource = new PersonResource(); resource.id = 1; resource.firstname = "Lee"; resource.lastname = "Woniper"; // 1. 기본 URI Link 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 추가 URI Link 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 List List list = new ArrayList<>(); list.add(new Link("http://localhost:8080/test1")); list.add(new Link("http://localhost:8080/test2")); list.add(new Link("http://localhost:8080/test3")); 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" // }
'Spring' 카테고리의 다른 글
spring boot-4(Velocity 설정과 사용) (0) | 2014.10.25 |
---|---|
spring boot-3(JPA 설정 및 사용) (12) | 2014.10.25 |
spring boot-2(프로젝트 구조와 Tomcat 연동 및 proerties사용) (7) | 2014.10.25 |
spring boot-1(특징과 기본 설정) (0) | 2014.10.25 |
이메일 전송 (0) | 2014.07.13 |
Comments