RestApi ๋ง๋ค๊ธฐ - ์ด๋ฒคํธ ์กฐํ API๊ตฌํ (10)
- ๊ฐ๋ฐ/restAPI๐ข
- 2021. 3. 9. 22:47
๊ณง ํ์ฌ์ผ์ด ๋ฐ๋น ์ง๋ค๊ณ ํ๋ค.
๋ฐ๋น ์ง๊ธฐ ์ ์ ์ด์ฌํ ๊ณต๋ถ ํ์!

๊ฐ๋จํ๊ฒ ํ ์คํธ ๋ถํฐ ์์ฑํด๋ณด์.
@Test
public void create_event() throws Exception {
Delivery delivery = generateEvent(100);
this.mockMvc.perform(get("/api/delivery/{id}",delivery.getId()))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("id").exists())
.andExpect(jsonPath("_links.self").exists())
.andExpect(jsonPath("_links.profile").exists());
}
์ด๊ฒ์ ์คํํ๋ฉด
์ด๋ค ์๋ฌ๊ฐ ๋ฐ์ํ ๊น?

์ 404๊ฐ ๋ฐ์ํ ๊น?
์๋ํ๋ฉด...
controller์ ๋ง๋ค์ง ์์๊ธฐ ๋๋ฌธ์ด๋ค.
๊ทธ์ ์ ๋ง์ฝ์ ์๋ชป๋ ์ ๋ณด๋ฅผ ์ ๋ ฅํ๊ฒ ๋๋ฉด ์ด๋ป๊ฒ ๋ ๊น?
@Test
public void create_event_bad() throws Exception {
this.mockMvc.perform(get("/api/delivery/9999999"))
.andDo(print())
.andExpect(status().isNotFound())
.andExpect(jsonPath("id").exists())
.andExpect(jsonPath("_links.self").exists())
.andExpect(jsonPath("_links.profile").exists());
}
๊ทธ๋ฌ๋ฉด ์ด๊ฑฐ๋ ์ฑ๊ณตํ ๊น?
์ฌ์ค..์ด๊ฒ๋ ์คํจํ๋ค.

์๋ํ๋ฉด ๋ฆฌ์์ค๋ฅผ ๋ง๋ค์ง ์์๊ธฐ ๋๋ฌธ์ด๋ค.
controller๋ฅผ ๋ง๋ค์.
@GetMapping("/{id}")
public ResponseEntity<?> getEvent(@PathVariable Integer id) {
Optional<Delivery> delivery = deliveryRepository.findById(id);
if (delivery.isEmpty()) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(delivery);
}
์ด๋ ๊ฒ ์์ฑํ๋ฉด ๋ ๊น?
์ ์ํ๊ฒ๋ ์ด๊ฑด ์ ๋ต์ด ์๋๋ค.
์๋ํ๋ฉด ๋ฆฌ์์ค๋ฅผ ๋ง๋ค์ง ์์๊ธฐ ๋๋ฌธ์ด๋ค.

๋ฆฌ์์ค๋ฅผ ๋ง๋ค์.
@GetMapping("/{id}")
public ResponseEntity<?> getEvent(@PathVariable Integer id) {
Optional<Delivery> optionalDelivery = deliveryRepository.findById(id);
if (optionalDelivery.isEmpty()) {
return ResponseEntity.notFound().build();
}
Delivery delivery = optionalDelivery.get();
EntityModel<Delivery> deliveryModel = DeliveryModel.modelOf(delivery);
return ResponseEntity.ok(deliveryModel);
}
์๊ฐํด๋ณด๋
public class DeliveryModel extends EntityModel<Delivery> {
public static EntityModel<Delivery> modelOf(Delivery delivery) {
EntityModel<Delivery> deliveryModel = EntityModel.of(delivery);
deliveryModel.add(linkTo(DeliveryController.class).withRel("query-events"));
deliveryModel.add(linkTo(DeliveryController.class).slash(delivery.getId()).withRel("update-events"));
deliveryModel.add(linkTo(DeliveryController.class).slash(delivery.getId()).withSelfRel());
return deliveryModel;
}
}
์ด๊ณณ์์ profile์ ๋นผ์ค์ผ ๋๋ค.
์๋ํ๋ฉด ์ฌ๊ธฐ์ ์กด์ฌํ๋ ๋งํฌ๋ค์ ๊ธ๋ก๋ฒ?๋งํฌ์ด๊ธฐ ๋๋ฌธ์...
๋นผ์ฃผ๋๊ฒ ๋ง๋๊ฒ ๊ฐ๋ค.
๊ทธ๋ฌ๋ฉด ํ
์คํธ๊ฐ ๋ง์ด ๊นจ์ง๊ฒ ๊ฐ์๋ใ
ใ
์๋ฌดํผ
์ด๊ฒ์ ์คํํด๋ณด๋ฉด

_links.profile์ด ์กด์ฌํ์ง ์๋๋ค๊ณ ๋์จ๋ค.
๋งํฌ๋ฅผ ์ถ๊ฐํด์ฃผ์.
์ด์ ํ๋กํ์ผ์ด ๋ญ์ง ์๊ฒ ๊ฐ๋ค.

์ด๋ ๊ฒ ์ถ๊ฐํ๋ฉด ๋ชจ๋ ์์ธ์ฒ๋ฆฌ๋ ๋ง์ณค๋ค.

์ด์ docs๋ฅผ ๋ง๋ค์ด๋ณด์.
์ด ๋ถ๋ถ์ request๋ถ๋ถ์ url๋ง ์กด์ฌํ๊ตฌ
๋๋จธ์ง๋ ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์ ๊ตณ์ด ๋ง๋ค์ง ์์๋ ๋ ๊ฒ ๊ฐ๋ค.
@Test
public void create_event() throws Exception {
Delivery delivery = generateEvent(10);
this.mockMvc.perform(get("/api/delivery/{id}", delivery.getId()))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("id").exists())
.andExpect(jsonPath("_links.self").exists())
.andExpect(jsonPath("_links.profile").exists())
.andDo(document("get-event",
links(linkWithRel("query-events").description("์ด๋ฒคํธ ์์ฑ"),
linkWithRel("update-events").description("์ด๋ฒคํธ ์์ "),
linkWithRel("self").description("ํด๋น ์ด๋ฒคํธ๋ก ์ด๋"),
linkWithRel("profile").description("ํด๋น ์ด๋ฒคํธ๋ก ์ด๋")),
responseHeaders(headerWithName(HttpHeaders.CONTENT_TYPE).description("ํ์ฌ contentType")),
responseFields(fieldWithPath("id").description("ํ์ฌ ์ด๋ฒคํธ ์์ด๋"),
fieldWithPath("item").description("์ํ๋ช
"),
fieldWithPath("user").description("ํ๋งค์"),
fieldWithPath("deliveryTime").description("์ถ๋ฐ ์๊ฐ"),
fieldWithPath("deliveryEndTime").description("๋์ฐฉ ์๊ฐ"),
fieldWithPath("status").description("๋ฒ ์ก ์ํ"),
fieldWithPath("itemPrice").description("๋ฌผํ ๊ฐ๊ฒฉ"),
fieldWithPath("deliveryCost").description("๋ฐฐ์ก๊ฐ๊ฒฉ"),
fieldWithPath("_links.query-events.href").description("์ด๋ฒคํธ ๋งํฌ๋ก ๊ฐ๊ธฐ"),
fieldWithPath("_links.update-events.href").description("์ด๋ฒคํธ ์์ ๋งํฌ ์ด๋"),
fieldWithPath("_links.self.href").description("ํด๋น ๋งํฌ๋ก ์ด๋ํ๊ธฐ"),
fieldWithPath("_links.profile.href").description("์ด๋ฒคํธ ํ๋กํ์ผ ๋งํฌ์ด๋ํ๊ธฐ"))));
}
์ด๊ฑฐ๋ ์๊ฐ๋ณด๋ค ์ด๋ ต์ง ์์๋ค.;;
์์ฑํ

'๊ฐ๋ฐ > restAPI๐ข' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| RestApi ๋ง๋ค๊ธฐ - ์คํ๋ง ์ํ๋ฆฌํฐ ์ ์ฉํ๊ธฐ (12) (0) | 2021.03.17 |
|---|---|
| RestApi ๋ง๋ค๊ธฐ - ์ด๋ฒคํธ ์์ API๊ตฌํ (11) (0) | 2021.03.11 |
| RestApi ๋ง๋ค๊ธฐ - ์ด๋ฒคํธ ๋ชฉ๋ก ์กฐํ API๊ตฌํ (9) (0) | 2021.03.06 |
| RestApi ๋ง๋ค๊ธฐ - API์ธ๋ฑ์ค ์ง์ ๋ง๋ค๊ธฐ (8) (0) | 2021.03.03 |
| RestApi ๋ง๋ค๊ธฐ - ๊ฐ์ข ๋ฌธ์ ์กฐ๊ฐ ์์ฑํ๊ธฐ(6) (0) | 2021.02.24 |