본문 바로가기
Dev/Spring Boot

[스프링 부트 개념과 활용] 스프링 웹 MVC 10부

by dev_jsk 2020. 8. 26.
728x90
반응형

Spring HATEOAS

Hypermedia As The Engine Of Application State

  • Server : 현재 리소스와 연관된 링크 정보를 Client에게 제공
  • Client : 연관된 링크 정보를 바탕으로 리소스에 접근
  • 연관된 링크 정보
    • Relation
    • Hypertext Reference

예시

// pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
// Hello.java

public class Hello {
    private String prefix;
    private String name;

    getter/setter
    toString()
}
// SampleController.java

@RestController
public class SampleController {
    @GetMapping(value="/hello")
    public Resource<Hello> hello() {
        Hello hello = new Hello();
        hello.setPrefix("Hey,");
        hello.setName("Jinseo");

        Resource<Hello> helloResource = new Resource<>(hello);
        helloResource.add(linkTo(methodOn(SampleController.class).hello()).withSelfRel());

        return helloResource;
    }
}

// SampleControllerTest.java

@RunWith(SpringRunner.class)
@WebMvcTest(SampleController.class)
public class SampleControllerTest {
    @Autowired
    MockMvc mockMvc;

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

결과

// Result

{
    "prefix":"Hey,"
    ,"name":"Jinseo"
    ,"_links":
    {
    	"self":{"href":"http://localhost/hello"}
    }
}

 

참고

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

ObjectMapper

제공하는 리소스를 JSON으로 변환할 때 사용하는 인터페이스

spring.jackson.* 속성들로 ObjectMapper를 커스터마이징 할 수 있다.

 

LinkDiscovers

Client에서 링크 정보를 Rel이름으로 찾을 때 사용할 수 있는 XPath를 확장해서 만든 HATEOAS용 Client API

728x90
반응형

댓글