Develop/Spring Boot
22. HATEOAS
자라선
2020. 7. 27. 17:00
HATEOS(헤이토스)
Hypermedia As The Engine Of Application State
로이필딩이 정의한 Rest API의 제약조건 중 하나로 클라이언트가 요청을 보낸 후 응답을 받았을때
응답받은 리소스와 연관된 하이퍼링크를 제공해야하는 조건이다.
HATEOAS를 적용한 응답 예시
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"application/hal+json"]
Content type = application/hal+json
Body = {"prefix":"Hey,","name":"thkong","_links":{"self":{"href":"http://localhost/hello"}}}
Forwarded URL = null
Redirected URL = null
Cookies = []
스프링 부트는 이러한 HATEOAS의 조건을 지키기 위하여 라이브러리를 제공해주고있다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
import org.springframework.hateoas.EntityModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
@RestController
public class SampleController {
@GetMapping("/hello")
public EntityModel<Hello> hello(){
Hello hello = new Hello();
hello.setPrefix("Hey,");
hello.setName("thkong");
EntityModel<Hello> helloResource = new EntityModel<>(hello);
helloResource.add(linkTo(methodOn(SampleController.class).hello()).withSelfRel());
return helloResource;
}
}
Hypermedia As The Engine Of Application State
· 서버: 현재 리소스와 연관된 링크 정보를 클라이언트에게 제공한다.
· 클라이언트: 연관된 링크 정보를 바탕으로 리소스에 접근한다.
· 연관된 링크 정보
o Relation
o Hypertext Reference)
· spring-boot-starter-hateoas 의존성 추가
· https://spring.io/understanding/HATEOAS
· https://spring.io/guides/gs/rest-hateoas/
· https://docs.spring.io/spring-hateoas/docs/current/reference/html/
ObjectMapper 제공
· spring.jackson.*
· Jackson2ObjectMapperBuilder
LinkDiscovers 제공
· 클라이언트 쪽에서 링크 정보를 Rel 이름으로 찾을때 사용할 수 있는 XPath 확장 클래스