RestApi ๋ง๋ค๊ธฐ - ๊ฐ์ข ๋ฌธ์ ์กฐ๊ฐ ์์ฑํ๊ธฐ(6)
- ๊ฐ๋ฐ/restAPI๐ข
- 2021. 2. 24. 23:27

์ค๋๋ง์ ์์ฑํ๋๊ฒ ๊ฐ๋ค.
์๋ฌดํผ ๋ค์ ์์ฑํ๊ฒ ๋์ด์ ๊ธฐ์๋ค.!
restAPI docs๋ฅผ ๋ง๋ค๊ธฐ ์ํด์๋
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
์ด ์์กด์ฑ์ ์ถ๊ฐํด์ค์ผ ํ๋ค.
RestApi ๋ง๋ค๊ธฐ - spring-rest-docs (5)
์ ๋๋์ด rest-docs๋ฅผ ์ ์ฉํ ๋๊ฐ ์๋ค. Spring REST Docs Document RESTful services by combining hand-written documentation with auto-generated snippets produced with Spring MVC Test. docs.spring.io..
b-programmer.tistory.com
์ด๊ธ์์ spring-rest-docs๋ฅผ ๋ง๋๋๊ฒ์ ์ค์ตํ๋ค.
์ด๋ฒ์๋ spring-rest-docs์์ ์ ๊ณต๋์ง ์์ ๋ฌธ์ ์กฐ๊ฐ์ ์์ฑํด๋ณด๋ ค๊ณ ํ๋ค.
์ด๊ธฐ ๋ฌธ์ ์กฐ๊ฐ๋ค

๋ด๊ฐ ๋ง๋ค ๋ฌธ์ ์กฐ๊ฐ๋ค์ ๋ค์๊ณผ ๊ฐ๋ค.
requestHeaders(),
requestFields(),
responseHeaders(),
responseFields()
์ผ๋จ ํด๋ณด์.(์ด๊ฑฐ ๋ง๊ณ ๋ง๋ค.)
requestHeaders๋
์์ฒญ header๋ฅผ ์์ฑํด์ฃผ๋ฉด ๋๋ค.
๊ทธ ๋ด์ฉ์ ํ
์คํธ๋ฅผ ๋๋ ค๋ณด๋ฉด ๋๋๋ฐ,
๋ค์๊ณผ ๊ฐ๋ค.
MockHttpServletRequest:
HTTP Method = POST
Request URI = /api/delivery/
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/hal+json", Content-Length:"137"]
Body = {"item":"book","user":"klom","deliveryTime":"2021-02-24T22:39:40.333417","deliveryEndTime":"2021-03-06T22:39:40.333437","itemPrice":null}
Session Attrs = {}
(์ด๊ฑด ์์ฑํ ์ฌ๋๋ง๋ค ๋ค๋ฅผ ๊ฒ๋๋ค.)
์ฌ๊ธฐ์ header์ ๋ฌด์์ด ๋ค์๋์ง ํ์ธํฉ๋๋ค.
Content-Type, Accpet, Content-Length ํ๊ธฐ!
headerWithName(HttpHeaders.CONTENT_TYPE).description("ContentType is application"),
headerWithName(HttpHeaders.ACCEPT).description("Accept is application/hal+json"),
headerWithName(HttpHeaders.CONTENT_LENGTH).description("137")
๋๋ ์ด๋ ๊ฒ ์์ฑํ๋ค. ๋ด์ฉ์ ์์ ๋กญ๊ฒ ์์ฑํ๋ฉด ๋๋ค.
์์ฒญ ๋ฌธ์ ์กฐ๊ฐ์ ์ด๋ ๊ฒ ์์ฑํ๋ค.
requestHeaders(
headerWithName(HttpHeaders.CONTENT_TYPE).description("ContentType is application"),
headerWithName(HttpHeaders.ACCEPT).description("Accept is application/hal+json"),
headerWithName(HttpHeaders.CONTENT_LENGTH).description("137")),
requestFields(fieldWithPath("item").description("what is your item"),
fieldWithPath("user").description("how is made"),
fieldWithPath("deliveryTime").description("start time"),
fieldWithPath("deliveryEndTime").description("end time"),
fieldWithPath("itemPrice").description("item price is"))
์ด์ ์๋ต๋ ์์ฑํ๊ธฐ ์ํด ์์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์์ฑํ์.
MockHttpServletResponse:
Status = 201
Error message = null
Headers = [Location:"http://localhost:8080/api/delivery/1", Content-Type:"application/hal+json"]
Content type = application/hal+json
Body = {"id":1,"item":"book","user":"klom","deliveryTime":"2021-02-24T22:39:40.333417","deliveryEndTime":"2021-03-06T22:39:40.333437","status":"READY","itemPrice":null,"deliveryCost":5000,"_links":{"query-events":{"href":"http://localhost:8080/api/delivery"},"update-events":{"href":"http://localhost:8080/api/delivery/1"},"self":{"href":"http://localhost:8080/api/delivery/1"}}}
Forwarded URL = null
Redirected URL = http://localhost:8080/api/delivery/1
Cookies = []
์์ฒญ๊ณผ ๋๊ฐ์ด ์์ฑํ๋ฉด ๋๋ค.
responseHeaders(headerWithName(HttpHeaders.LOCATION).description("Location!"),
headerWithName(HttpHeaders.CONTENT_TYPE).description("Content-Type is application/hal+json")),
responseFields(fieldWithPath("id").description("this item id"),
fieldWithPath("item").description("what is your item"),
fieldWithPath("user").description("what your name"),
fieldWithPath("deliveryTime").description("start time"),
fieldWithPath("deliveryEndTime").description("end time"),
fieldWithPath("status").description("current status"),
fieldWithPath("itemPrice").description("item price is "),
fieldWithPath("deliveryCost").description("delivery cost")))
์ด๋ ๊ฒ ์คํํ๋ฉด ๋ ๊น?
๋๋ ค๋ณด์.

์ด๋์ ์๋ชป๋๊ฒ์ผ๊น?
๋๋ ๋ถ๋ช
๋ค ์์ฑํ... ์ด๋ผ..
์๊ฐํด๋ณด๋ ์๋ต ์ฝ๋์๋ links๊ฐ ์ถ๊ฐ๋์๋ค๋ ์ฌ์ค์ ์๊ณ ์์๋ค.
์ด๊ฒ์ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ 2๊ฐ์ง๋ฅผ ์๊ฐํ๊ฒ ๋ค.
relaxedResponseFields์ผ๋ก ์์ ํ๋ค.
์ด ๋ฐฉ๋ฒ์ ํ๊ฒ๋๋ฉด ๋ด๊ฐ ์
๋ ฅํ ๋ถ๋ถ๋ง ๋ฌธ์๋ก ๋ง๋ค์ด ์ค๋ค.
ํ์ง๋ง ๋ด๊ฐ ์ค์๋ก ๋ฌด์ธ๊ฐ๋ฅผ ๋นผ๋จน๊ณ ์คํํ๋ค๋ฉด,
๊ทธ๋๋ ๋ฌธ์๋ฅผ ๋ง๋ค์ด์ค๋ค. ์ ์ฌ์ง์์ ํ์ธํ๊ฒ ์ฒ๋ผ ์๋ ์๋ตํน์ ์์ฒญ์๋ ์กด์ฌํ์ง๋ง ๋ฌธ์๋ก ๋ง๋ค์ง ์๋ ๊ฒฝ์ฐ, ์ ์ฒ๋ผ ์๋ ค์ค๋ค.
์ฅ์ : ๋ด๊ฐ ์
๋ ฅํ ๊ฒ๋ง ๋ฌธ์๋ก ๋ง๋ค์ด์ค๋ค.
๋จ์ : ์ค์๋ฅผ ๋ฐฉ์งํด์ฃผ์ง ์๋๋ค.
๋ค์์ ์์ฑํ๋ค.
fieldWithPath("_links.*.*").description("link information")))
์ด๋ ๊ฒ ์ ๋ ฅํ๋ฉด ๋ฌธ์๋ก ๋ง๋ค์ด์ค๋ค.
์ฌ์ง ๊ฐ์ ๊ณ์ธต์ผ๋ก ๊ตฌ์ฑ๋์๊ธฐ ๋๋ฌธ์ ํน์... ๋๋ ํ๋ ๋ง์์ผ๋ก ํ๋๋ฐ ๋คํํ ๋ง๋ค์ด์ก๋ค.
์ด๋ ๊ฒ ๋ง๋ค์ด์ง ๋ฌธ์ ์กฐ๊ฐ๋ค์

์ด๋ฐ์์ผ๋ก ์๋กญ๊ฒ ์ถ๊ฐ๋์๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
'๊ฐ๋ฐ > restAPI๐ข' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| RestApi ๋ง๋ค๊ธฐ - ์ด๋ฒคํธ ๋ชฉ๋ก ์กฐํ API๊ตฌํ (9) (0) | 2021.03.06 |
|---|---|
| RestApi ๋ง๋ค๊ธฐ - API์ธ๋ฑ์ค ์ง์ ๋ง๋ค๊ธฐ (8) (0) | 2021.03.03 |
| RestApi ๋ง๋ค๊ธฐ - spring-rest-docs (5) (0) | 2021.02.19 |
| RestApi ๋ง๋ค๊ธฐ - HATEOAS (5) (0) | 2021.02.16 |
| RestApi ๋ง๋ค๊ธฐ - badRequest (4) (0) | 2021.02.13 |