본문 바로가기

Java/Spring Boot

[Spring Boot] Spring HATEOAS

1. HATEOAS

  • Hypermedia As The Engine Of Application State
  • 서버: REST API에서 리소스에 대한 정보를 제공할 때, 현재 리소스와 연관된 링크 정보를 클라이언트에게 제공한다.
  • 클라이언트: 연관된 링크 정보를 바탕으로 리소스에 접근한다.
  • 연관된 링크 정보
    - Relation(rel)
    - Hypertext Reference(href)
  • spring-boot-starter-hateoas 의존성 추가
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
  • ObjectMapper 제공
    - spring.jackson.* 사용해서 커스터마이징 가능 (application.properties)
    - Jackson2ObjectMapperBuilder
    - 여러가지 이유로 많이 사용함
    - 우리가 제공하는 리소스를 json으로 변환할 때 사용하는 인터페이스
    - jackson 라이브러리에서 제공하는 클래스
    - spring-boot-starter-web만 들어있어도 bean으로 등록해줌
    - 따로 등록할 필요없이 주입받아서 사용하면 됨
    @Autowired
    ObjectMapper objectMapper;
  • LinkDiscovers 제공
    - 클라이언트 쪽에서 링크 정보를 Rel 이름으로 찾을때 사용할 수 있는 XPath 확장 클래스
    - 직접 사용할 일은 많지 않음

 

 

# SampleController

@RestController
public class SampleController {

   @GetMapping("/hello")
   public EntityModel<Hello> hello() {
      Hello hello = new Hello();
      hello.setPrefix("Hey,");
      hello.setName("hhlin");

      EntityModel<Hello> helloResource = EntityModel.of(hello);
      // SampleController 클래스에서 제공하는 hello라는 메소드에 대한 링크를 따서
      // 이 링크를 self relation으로 만들어서 추가
      helloResource.add(linkTo(methodOn(SampleController.class).hello()).withSelfRel());

      return helloResource;
   }
}

 

# SampleControllerTest

@RunWith(SpringRunner.class)
@WebMvcTest(SampleController.class)
public class SampleControllerTest {

   @Autowired
   MockMvc mockMvc;

   @Test
   public void hello() throws Exception{
      mockMvc.perform(get("/hello"))
         .andDo(print())
         .andExpect(status().isOk())
         .andExpect(jsonPath("$._links.self").exists());
   }
}

 

# Hello

public class Hello {

   private String prefix;
   private String name;
   
   // getter, setter, toString 생략

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8/dashboard

 

스프링 부트 개념과 활용 - 인프런 | 강의

스프링 부트의 원리 및 여러 기능을 코딩을 통해 쉽게 이해하고 보다 적극적으로 사용할 수 있는 방법을 학습합니다., - 강의 소개 | 인프런...

www.inflearn.com

https://spring.io/understanding/HATEOAS

https://spring.io/guides/gs/rest-hateoas/

 

Building a Hypermedia-Driven RESTful Web Service

this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and techniques as recommended by the Spring team

spring.io

https://docs.spring.io/spring-hateoas/docs/current/reference/html/

 

Spring HATEOAS - Reference Documentation

Example 47. Configuring WebTestClient when using Spring Boot @SpringBootTest @AutoConfigureWebTestClient (1) class WebClientBasedTests { @Test void exampleTest(@Autowired WebTestClient.Builder builder, @Autowired HypermediaWebTestClientConfigurer configure

docs.spring.io

https://haruhiism.tistory.com/200

 

Spring HATEOAS를 이용한 REST

이 포스트는 스프링의 튜토리얼을 실습하는 내용이다. Building REST services with Spring this tutorial is designed to be completed in 2-3 hours, it provides deeper, in-context explorations of enterpris..

haruhiism.tistory.com

 

'Java > Spring Boot' 카테고리의 다른 글

[Spring Boot] 인-메모리 데이터베이스  (0) 2022.04.10
[Spring Boot] CORS  (0) 2022.04.03
[Spring Boot] ExceptionHandler  (0) 2022.04.03
[Spring Boot] index 페이지와 favicon  (0) 2022.04.03
[Spring Boot] 웹 JAR  (0) 2022.04.03