본문 바로가기
Spring Boot

HATEOAS

by 상국이 2022. 2. 28.
728x90

HATEOAS?

 > Hypermedia As Engine Of Application State

 > resource와 연관있는 정보를 같이 제공(서버)하고 현재 resource와 연결된 링크 정보를 바탕으로 resource에 접근(클라이언트)

 

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

 

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

 

예제

Controller

@RestController
public class SampleController {

    @GetMapping("/hello")
    public EntityModel<Hello> hello(){
        Hello hello =  Hello.builder()
                .prefix("Hi, ")
                .name("sangguk")
                .build();

        EntityModel<Hello> helloEntityModel = EntityModel.of(hello);
        helloEntityModel.add(linkTo(methodOn(SampleController.class).hello()).withSelfRel());

        return helloEntityModel;
    }
}

 

TestCode

@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());
    }
}

 

* ObjectMapper 

 > 객체 > json 변환 혹은 json > 객체 로 변환 시 사용하는 mapper

 > 커스터마이징은 spring.jackson.* 으로 properties를 지정해줄 수 있음

 

728x90

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

인메모리 데이터베이스  (0) 2022.03.05
CORS  (0) 2022.02.28
ExceptionHandler  (0) 2022.02.28
문법  (0) 2022.02.28
HtmlUnit  (0) 2022.02.28

댓글