RestApi ๋ง๋ค๊ธฐ - spring-rest-docs (5)
- ๊ฐ๋ฐ/restAPI๐ข
- 2021. 2. 19. 23:25

์ ๋๋์ด 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
์๋
private MockMvc mockMvc;
@Autowired
private WebApplicationContext context;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation))
.build();
}
์ด๋ฌํ ์์
์ ์งํํด์ผ ๋์ง๋ง,
์คํ๋ง ๋ถํธ์์๋
@AutoConfigureRestDocs
class DeliveryTest {
์ด ์ด๋ ธํ ์ด์ ์ด๋ฉด ์ ์ฝ๋๋ฅผ ์์ฑํด์ค๋ค๊ณ ํ๋ค.
์ด์ ๋ง๋ค๋ ค๊ณ ํ๋ ํ ์คํธ์ฝ์ค์
.andDo(document("create-events"))
์ด๋ฌํ ๋ด์ฉ์ ๋ฃ์ด์ค๋ค.
create-events๋ ์๋ณ์๋ก ์ ๊ธฐ์๋ ์๋ฌด๊ฑฐ๋ ๋ฃ์ด๋ ์๊ด์๋ค.
๊ทธ๋ฆฌ๊ณ ์คํํ๊ฒ ๋๋ฉด,

์ด๋ฌํ ๋ด์ฉ์ด ๋ค์ด์๋ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
๋ด์ฉ์ ๋ณด์.

์ฌ๋ฌ๊ธ ์ค curl-request ์ฆ, curl ์์ฒญ ์ ๋ณด๋ฅผ ํ์ธ ํ ์ ์๋ค.
itemPrice๊ฐ null์ธ๊ฒ ์ข ๊ทธ๋ ์ง๋ง.. ์๋ฌดํผ
๊ทผ๋ฐ ๋๋ ์ต๋ํ ์งง๊ฒ ํด์ ๊ทธ๋ฆฌ ๋ณด๊ธฐ ์ด๋ ต์ง๋ ์์ง๋ง json์ด ๊ธธ๋ค๊ณ ์๊ฐํด๋ณด์.
๊ทธ๋ฌ๋ฉด ์์๋ณด๊ธฐ๊ฐ ์ด๋ ต๋ค.
๋ฐฉ๋ฒ์ ์ฐพ์์ผ ๋๋ค.
๋คํํ
spring-docs์๋ json์ ๋ณด๋ฅผ ํฌ๋งทํ
ํ๋ ์ฌ๋ฌ๊ฐ์ง ํ๋ก์์ ๋ค์ด ์กด์ฌํ๋ค.
์ด๊ฒ์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ RestDocsMockMvcConfigurationCustomizer์ ๋ฑ๋กํ ํ์๊ฐ ์๋ค.
์ด์ฐจํผ ์ด๊ฑด ํ
์คํธ ์ฝ์ค์์๋ง ์ฌ์ฉํ ์์ ์ด๋ฏ๋ก
ํ
์คํธ์ชฝ์ ํจํค์ง๋ฅผ ๋ง๋ค๊ตฌ ํด๋์ค๋ฅผ ๋ง๋ค์๋ค.
๊ทธ๋ฆฌ๊ตฌ,
@TestConfiguration
public class RestDocsConfiguration {
์ด์ ์ด๊ฑด ํ ์คํธ์ฉ Configuration๋ผ๋ ๋ป์ด๋๋ค.
์ด์ ๋ง๋ค์ด๋ณด์.
๋น์ผ๋ก ๋ฑ๋กํ๊ตฌ
@Bean
public RestDocsMockMvcConfigurationCustomizer restDocsMockMvcConfigurationCustomizer() {
}
configurer.operationPreprocessors().withResponseDefaults().withRequestDefaults();
์ค์ -> ํ๋ก์์ ์ด๋?ใ
-> ๊ธฐ๋ณธ ์๋ต ์ธํ
-> ๊ธฐ๋ณธ ์์ฒญ ์ธํ
์ฐธ๊ณ ๋ก ์๋ฌด๊ฒ๋ ์์ด๋ ์ปดํ์ผ์๋ฌ๋ ๋ฐ์ํ์ง ์๋๋ค.

์ด๊ฒ์ ๋ดค์๋, ํ๋ก์์ ๋ ํ๋๋ง ์
๋ ฅํ ํ์๊ฐ ์๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
์ด์ ๊ณต์ ๋ฌธ์์์ ํ๋์ฉ ์ฌ์ฉํ๋ฉด์ ์ด๋ป๊ฒ ๋ณํ๋์ง ์ง์ ๋์ผ๋ก ํ์ธํด๋ณด์.
๊ทธ ์ ์ ํด์ผ๋ ๊ฒ์ด์๋ค.
์ฌ์ค ์ด๊ฒ๋ง ํ๋ฉด ๋์ง ์๋๋ค.
@Import(RestDocsConfiguration.class)
์ด๋ ๊ฒ ์ํฌํธ๋ฅผ ์์ผ์ค๋ค์...
configurer.operationPreprocessors().withResponseDefaults(prettyPrint()).withRequestDefaults(prettyPrint());
์๋ต,์์ฒญ ํ๋ก์์ => prettyprint์ผ๋

์ ๋ฉ์์ง๊ฐ ํ์คํ ์์๊ฒ ๋์จ๋ค.
์๋ต, ์์ฒญ ํ๋ก์์ => Masking Links

๋ญ๊ฐ ๋ฌ๋ผ์ก์ง...
์ฌ์ค ์ฌ๊ธฐ๋ ํฐ ์๋ฏธ๋ ์๋ ๊ฒ ๊ฐ๋ค. ์๋ํ๋ฉด ์ฃผ์๊ฐ ์์ผ๋๊น
์ด๊ฑด ๋ชจ๋ ์ฃผ์๋ฅผ href์ ๊ฐ์ ํจ์ด ์๋ค๊ณ ํ๋ค.

์ด๊ฒ...

์ํฌ๋ฆฟ ๋ชจ๋์ธ๊ฐ...
์ด๊ฑด ๋ชจ๋ฅด๊ฒ ๋ค.
์๋ต, ์์ฒญ ํ๋ก์์ => Removing Headers
๋๊ฐ ๋ด๋ ํค๋๋ฅผ ์ง์์ฃผ๋ ๊ฒ ๊ฐ๋ค.


์ ํค๋ ์ง์์ค๋ฐ๋งค...
์๊ณ ๋ณด๋ ์ง์ ๋ ํค๋ ์ด๋ฆ๊ณผ ๋์ผํ ๋ชจ๋ ํค๋๊ฐ ์์ฒญ ๋๋ ์๋ต์์ ์ ๊ฑฐ๋ฉ๋๋ค. ๋ผ๊ณ ํฉ๋๋ค.ใ ใ
๋์ค์ ๋ค์ ํด๋ด์ผ์ง...
์ด? ๋ค๋ฅธ๊ฒ ๋ ์๋ค
removeMatchingHeaders

์ด๊ฑด๋ญ..
์ด๊ฑด ์ด๋ฐ์์ผ๋ก
configurer -> configurer.operationPreprocessors().withResponseDefaults(removeMatchingHeaders("[a-zA-Z]"))
.withRequestDefaults(removeMatchingHeaders("[a-zA-Z]"));
์ ๊ท์์ ๋ฃ์ ์ ์๋ค.
์ด๊ฒ๋ ๋น์ฅ์ ์ฐ๊ธฐ๋ ํ๋ค๊ฒ ๊ฐ๊ตฌ, ๋์ค์ ๊ณต๋ถํ๊ณ ์ฌ์ฉํด๋ด์ผ ๊ฒ ๋ค.
์ด๋ฐ์๋
Replacing Patterns ์ด๋ Modifying Request Parameters๋ ์กด์ฌํ๋ค.
์์งํ ๋งจ ์ฒ์ ๋นผ๊ณ ๋ ๊ฐํฅ์ด ์๋ค.
์๋ฌดํผ

์ด๊ฒ ๊ธฐ๋ณธ์ผ๋ก ๋ง๋ค์ด์ฃผ๋ ์ค๋ ํ์ด๋ผ๊ณ ํ๋ค.
'๊ฐ๋ฐ > restAPI๐ข' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| RestApi ๋ง๋ค๊ธฐ - API์ธ๋ฑ์ค ์ง์ ๋ง๋ค๊ธฐ (8) (0) | 2021.03.03 |
|---|---|
| RestApi ๋ง๋ค๊ธฐ - ๊ฐ์ข ๋ฌธ์ ์กฐ๊ฐ ์์ฑํ๊ธฐ(6) (0) | 2021.02.24 |
| RestApi ๋ง๋ค๊ธฐ - HATEOAS (5) (0) | 2021.02.16 |
| RestApi ๋ง๋ค๊ธฐ - badRequest (4) (0) | 2021.02.13 |
| RestApi ๋ง๋ค๊ธฐ - ์ ๋ ฅ๊ฐ ์ ํํ๊ธฐ + ์ ๋ ฅ๊ฐ ์ด์ธ์ ์๋ฌ ๋ฐ์ (3) (0) | 2021.02.09 |