Spring HATEOAS
Last updated
Last updated
@RestController
public class SampleController {
@GetMapping("/hateoas")
public EntityModel<Hello> hello() {
Hello hello = new Hello();
hello.setPrefix("Hey, ");
hello.setName("keesun");
// 링크 추가
EntityModel<Hello> helloResource = EntityModel.of(hello);
helloResource.add(linkTo(methodOn(SampleController.class).hello()).withSelfRel());
return helloResource;
}
}@WebMvcTest(SampleController.class)
class SampleControllerTest {
@Autowired
MockMvc mockMvc;
@Test
void hello() throws Exception {
mockMvc.perform(get("/hateoas"))
.andDo(print())
.andExpect(status().isOk())
// 링크 정보 확인
.andExpect(jsonPath("$._links.self").exists());
}
}{
"prefix": "Hey, ",
"name": "keesun",
"_links": {
"self": {
"href": "http://localhost/hateoas"
}
}
}