HATEOAS ๋?
Hypermedia As The Engine Of Application State ์ ์ฝ์์ด๋ค.
rest API์์,
์๋ฒ๋ ํ์ฌ ๋ฆฌ์์ค์ ์ฐ๊ด๋ ๋งํฌ ์ ๋ณด๋ฅผ ํด๋ผ์ด์ธํธ์๊ฒ ์ ๊ณตํ๋ค.
ํด๋ผ์ด์ธํธ๋ ์ฐ๊ด๋ ๋งํฌ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ๋ฆฌ์์ค์ ์ ๊ทผํ๋ค.
์ฐ๊ด๋ ๋งํฌ ์ ๋ณด: Relation Hypertext Reference;
HATEOAS ๊ฐ๋จํ๊ฒ ์ฌ์ฉํด๋ณด๊ธฐ
spring-boot-starter-hateoas ์์กด์ฑ ์ถ๊ฐ
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
Entity ์์ฑ
@Getter
@Setter
@noargsconstructor
public class Hello {
private String prefix;
private String name;
}
Controller ์์ link ์ฐ๊ฒฐ
@RestController
public class SampleController {
@GetMapping("/hello")
public EntityModel<Hello> hello(){
Hello hello = new Hello();
hello.setPrefix("Hey,");
hello.setName("jueun");
EntityModel<Hello> helloEntity;
helloEntity = EntityModel.of(hello,
linkTo(methodOn(SampleController.class).hello()).withSelfRel());
return helloEntity;
}
}
์ ์ผ ๊ฐ๋จํ๊ฒ ํธ์ถ๋ self์ ๋งํฌ๋ฅผ ์ฐ๊ฒฐํด๋ณด์๋ค.
Test ํด๋ณด๊ธฐ
@RunWith(SpringRunner.class)
@WebMvcTest(SampleController.class)
public class SampleControllerTest {
@Autowired
MockMvc mockMvc;
// @Autowired
// ObjectMapper objectMapper;
@Test
public void hello() throws Exception {
mockMvc.perform(get("/hello"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$._links.self").exists());
}
}
์ถ๊ฐ๋ก, hateoas๊ฐ ์๋ web๋ง ์ถ๊ฐํ๋๋ผ๋ ์ ๊ณตํด์ฃผ๋ ObjectMapper ๊ทธ๋ฅ ์ฃผ์ ํด์ ์ธ ์ ์๋ค.
๊ฐ์ฒด๋ฅผ json์ผ๋ก ๋ณํํ๊ฑฐ๋ json์ ๊ฐ์ฒด๋ก ๋ณํํ ๋ ์ฌ์ฉํ๋ ๋งตํผํด๋์ค์ด๋ค.
์ปค์คํฐ๋ง์ด์ง์ ์ค์ ํ์ผ์์ spring.jackson.* ์ดํ์ ์ต์ ๋ค์ ํตํด ํ ์ ์๋ค.
๋ง์ง๋ง์ผ๋ก ๋ง๋ถ์ฌ์, HATEOAS๋ LinkDiscovers ์ ๊ณตํ๋ค.
ํด๋ผ์ด์ธํธ ์ชฝ์์ ๋งํฌ ์ ๋ณด๋ฅผ Rel ์ด๋ฆ์ผ๋ก ์ฐพ์ ๋ ์ฌ์ฉํ ์ ์๋ XPath ํ์ฅ ํด๋์ค๋ผ๊ณ ํ๋ค.
Building a Hypermedia-Driven RESTful Web Service (๊ณต์๋ฌธ์) #
HATEOAS ๋ ํผ๋ฐ์ค #
'Springboot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[spring boot] ์์ฃผ ๊ฐ๋จํ๊ฒ Thymeleaf ์ฌ์ฉํด๋ณด๊ธฐ (1) | 2021.01.16 |
---|---|
[spring boot] index.html, favicon (0) | 2021.01.10 |
[spring boot] WebJars ์ค jquery ์ถ๊ฐํด๋ณด๊ธฐ (0) | 2021.01.10 |
[spring boot] ์ ์ ๋ฆฌ์์ค ์ง์ (0) | 2021.01.10 |
[spring boot] HttpMessageConverters (0) | 2021.01.10 |
[spring boot] Test (0) | 2021.01.09 |