RestApi ๋ง๋ค๊ธฐ - ์ด๋ฒคํธ ์์ API๊ตฌํ (11)
- ๊ฐ๋ฐ/restAPI๐ข
- 2021. 3. 11. 22:59

์ด๋ฒ์๋ ๊ฐ์๋ฅผ ๋ฃ๊ธฐ์ ์ ๋จผ์ ์์ฑํ๊ธฐ๋ก ํด๋ณด์.
์ด๋ฒคํธ ์์ ์ ๊ฒฝ์ฐ๋
1. ์์ ์ด ์ ์์ ์ผ๋ก ์คํ์ด ๋์์๋ : 200
2. ์์ ์ด ์๋ชป๋ ๊ฒฝ์ฐ
- ๋น์ด์๋ ๊ฐ์ฒด๊ฐ ๋ค์ด์์ ๊ฒฝ์ฐ.
- ์๋ชป๋ ๊ฐ์ฒด๊ฐ ๋ค์ด์ฌ ๊ฒฝ์ฐ..
- ๊ถํ์ด ์๋ ์ฌ๋์ด ์ ๊ทผํ์ ๊ฒฝ์ฐ
- ํ์ด์ง๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ
์ด๋ ๊ฒ ๋ค ์ ์๋๋ฐ... ์์งํ 2-3์ ์ง๊ธ๋น์ฅ์ ํ์ง ๋ชปํ ๊ฒ ๊ฐ๋ค.
์๋ํ๋ฉด ์ ์ ๋ฅผ ๊ณ ๋ คํ์ง ์์๊ธฐ ๋๋ฌธ์ด๋ค.
ํ
์คํธ ์ฝ๋๋ฅผ ์์ฑํด๋ณด์.
@Test
void update_event() throws Exception {
Delivery delivery = generateEvent(100);
DeliveryDto deliveryDto = modelMapper.map(delivery, DeliveryDto.class);
deliveryDto.setItem("book");
deliveryDto.setUser("klom2");
deliveryDto.setItemPrice(1000);
deliveryDto.setDeliveryTime(LocalDateTime.now());
deliveryDto.setDeliveryEndTime(LocalDateTime.now().plusDays(10));
this.mockMvc.perform(put("/api/delivery")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(deliveryDto)))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("id").exists())
.andExpect(jsonPath("user").value("klom2"))
.andExpect(jsonPath("_link.self").exists())
.andExpect(jsonPath("_link.profile").exists());
}
์ ์์ ์ธ ๊ฒฝ์ฐ ํ ์คํธ์ ์ฑ๊ณตํ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ๋ง๋ค์ด๋ณด์.
@PutMapping("/{id}")
public ResponseEntity<?> updateEvent(@PathVariable Integer id) {
return ResponseEntity.ok().build();
}
์ด๋ ๊ฒ ๋ง๋ค๋ฉด ๋ ๊น?
ํ์ง๋ง ์ด๋ ๊ฒ ๋ง๋ค๋ฉด... id๋ฅผ ์
๋ ฅํ์ง ์๊ธฐ ๋๋ฌธ์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
๋ฐ๋ผ์..
@PutMapping("/{id}")
public ResponseEntity<?> updateEvent(@PathVariable Integer id) {
Optional<Delivery> optionalDelivery = deliveryRepository.findById(id);
Delivery delivery = optionalDelivery.get();
EntityModel<Delivery> entityModel = DeliveryModel.modelOf(delivery);
return ResponseEntity.ok(entityModel);
}
์ด๋ ๊ฒ ์์ ํ๋ค.
ํ์ง๋ง ์ด๋ ๊ฒ ๋ง๋๋ ์๋ก์ด ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.

๊ทธ๋์ ์ด๋ ๊ฒ ์์ ํ๋ค.
@PutMapping("/{id}")
public ResponseEntity<?> updateEvent(@PathVariable Integer id,
@RequestBody @Valid DeliveryDto deliveryDto,Errors errors) {
Optional<Delivery> optionalDelivery = deliveryRepository.findById(id);
Delivery delivery = optionalDelivery.get();
Delivery map = modelMapper.map(deliveryDto, Delivery.class);
Delivery newDelivery = deliveryRepository.save(map);
EntityModel<Delivery> entityModel = DeliveryModel.modelOf(newDelivery);
return ResponseEntity.ok(entityModel);
}
์ด์ ํ๋กํ์ผ์ ์ถ๊ฐํด๋ณด๋ฉด...

ํ
์คํธ๊ฐ ์ฑ๊ณตํ๋ค.
์ด์ ๋ค์ ๋ฏธ์
์์ ์ด ์๋ชป๋์์ ๊ฒฝ์ฐ๋ฅผ ์๊ฐํด๋ณด์.
๋น์ด์๋ ๊ฐ์ผ ๊ฒฝ์ฐ์๋
if (errors.hasErrors()) {
return errorResource(errors);
}
์ด๊ฒ์ ์ถ๊ฐํด์ฃผ๋ฉด ๋์๋ค.
๊ฐ์ด ์๋ชป๋ ๊ฒฝ์ฐ์๋
deliveryValidator.validate(deliveryDto, errors);
if (errors.hasErrors()) {
return errorResource(errors);
}
์ด๊ฑธ ์ถ๊ฐํ๋ฉด ๋์๋ค.!
์ต์ข
์ ์ผ๋ก
@PutMapping("/{id}")
public ResponseEntity<?> updateEvent(@PathVariable Integer id,
@RequestBody @Valid DeliveryDto deliveryDto,
Errors errors) {
Optional<Delivery> optionalDelivery = deliveryRepository.findById(id);
if (errors.hasErrors()) {
return errorResource(errors);
}
deliveryValidator.validate(deliveryDto, errors);
if (errors.hasErrors()) {
return errorResource(errors);
}
Delivery delivery = optionalDelivery.get();
Delivery map = modelMapper.map(deliveryDto, Delivery.class);
Delivery newDelivery = deliveryRepository.save(map);
EntityModel<Delivery> entityModel = DeliveryModel.modelOf(newDelivery);
entityModel
.add(Link.of("http://localhost:8080/docs/index.html#update-event").withRel("profile"));
return ResponseEntity.ok(entityModel);
}
ํ ๊ทผ๋ฐ.. ๋๋ฌด ๋๋ฝ๊ธด ๋๋ฝ๋ค...ใ
ใ
์ด๋ฐ ๋ฆฌํํ ๋ง์ ํ์.
์ด์ doc์ผ๋ก ๋ง๋ค๊ณ ๋ง์น์..
@Test
void update_event() throws Exception {
Delivery delivery = generateEvent(100);
DeliveryDto deliveryDto = modelMapper.map(delivery, DeliveryDto.class);
deliveryDto.setItem("book");
deliveryDto.setUser("klom2");
deliveryDto.setItemPrice(1000);
deliveryDto.setDeliveryTime(LocalDateTime.now());
deliveryDto.setDeliveryEndTime(LocalDateTime.now().plusDays(10));
this.mockMvc.perform(put("/api/delivery/{id}", delivery.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(deliveryDto)))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("id").exists())
.andExpect(jsonPath("user").value("klom2"))
.andExpect(jsonPath("_links.self").exists())
.andExpect(jsonPath("_links.profile").exists())
.andDo(document("update-event",
links(linkWithRel("query-events").description("์ด๋ฒคํธ ์์ฑํ์ด์ง๋ก"),
linkWithRel("update-events").description("์ด๋ฒคํธ ์์ ํ์ด์ง๋ก"),
linkWithRel("self").description("ํด๋น ์ด๋ฒคํธ๋ก ์ด๋ํ์ด์ง๋ก"),
linkWithRel("profile").description("ํด๋น ์ด๋ฒคํธ๋ก ์ด๋ํ์ด์ง๋ก")),
requestHeaders(headerWithName(HttpHeaders.CONTENT_TYPE).description("ํ์ฌ contentType"),
headerWithName(HttpHeaders.CONTENT_LENGTH).description("content ๊ธธ์ด")),
requestFields(fieldWithPath("item").description("์ํ๋ช
"),
fieldWithPath("user").description("ํ๋งค์"),
fieldWithPath("deliveryTime").description("ํ๋งค์ผ"),
fieldWithPath("deliveryEndTime").description("๋์ฐฉ์ผ"),
fieldWithPath("itemPrice").description("์ํ๊ฐ๊ฒฉ")),
responseHeaders(headerWithName(HttpHeaders.CONTENT_TYPE).description("ํ์ฌ ์ฌ์ฉํ๊ณ ์๋ contentType")),
responseFields(fieldWithPath("id").description("this item id"),
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("ํ์ฌ ๋งํฌ๋ก ์ด๋"))));
}
์ด๋ง์ด๋งํ๊ตฐ..

์ค๋ณต๋ ๋ถ๋ถ๊ณผ ์ธ๋ชจ์๋ ๋ถ๋ถ์ด ์กฐ๊ธ ๋ณด์ธ๋ค.
์ด์ ๋ฆฌํํ ๋ง์ ํด์ผ๋๋ค.
์ด๊ฑฐ๋ ์ฌ๋ฆฌ์ง ์์ ์์ ...
๊ทธ๋ฆฌ๊ณ ๋์ ์ ์ ๋ ์ถ๊ฐํด์ฃผ๋ฉด resapi๋!
'๊ฐ๋ฐ > restAPI๐ข' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| RestApi ๋ง๋ค๊ธฐ - ์คํ๋ง ์ํ๋ฆฌํฐ ์ค์ (13) (0) | 2021.03.23 |
|---|---|
| RestApi ๋ง๋ค๊ธฐ - ์คํ๋ง ์ํ๋ฆฌํฐ ์ ์ฉํ๊ธฐ (12) (0) | 2021.03.17 |
| RestApi ๋ง๋ค๊ธฐ - ์ด๋ฒคํธ ์กฐํ API๊ตฌํ (10) (0) | 2021.03.09 |
| RestApi ๋ง๋ค๊ธฐ - ์ด๋ฒคํธ ๋ชฉ๋ก ์กฐํ API๊ตฌํ (9) (0) | 2021.03.06 |
| RestApi ๋ง๋ค๊ธฐ - API์ธ๋ฑ์ค ์ง์ ๋ง๋ค๊ธฐ (8) (0) | 2021.03.03 |