✏️ 환경설정
📍 build.gradle
plugins
id 'org.asciidoctor.jvm.convert' version '3.3.2'
}
configurations {
asciidoctorExt
}
ext {
set('snippetsDir', file("build/generated-snippets"))
}
dependencies {
asciidoctorExt 'org.springframework.restdocs:spring-restdocs-asciidoctor'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
}
tasks.named('test') {
outputs.dir snippetsDir
}
tasks.named('asciidoctor') {
inputs.dir snippetsDir
dependsOn test
}
asciidoctor {
dependsOn test
configurations 'asciidoctorExt'
baseDirFollowsSourceFile()
}
tasks.register('copyApiDocument', Copy) {
dependsOn asciidoctor
doFirst {
delete file("src/main/resources/static/docs")
}
from asciidoctor.outputDir
into file("src/main/resources/static/docs")
}
✏️ 사용하기
📍 MockMvc
- 기본 세팅
- @AutoConfigureRestDocs 선언
- document() : 문서 작업 시작
- preprocessRequest, Response(prettyPrint()) : 요청, 응답 json 을 출력할 때 줄을 맞춰줌
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
...
@AutoConfigureRestDocs
class test {
...
mockMvc.perform(MockMvcRequestBuilders
.get("/restdocs"))
.andExpect(status().is2xxSuccessful())
.andDo(document("docs_name",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint());
```java
import static org.springframework.restdocs.cookies.CookieDocumentation.cookieWithName;
import static org.springframework.restdocs.cookies.CookieDocumentation.requestCookies;
...
.andDo(document("docs_name",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
requestCookies(
cookieWithName("key").description("about")
)
- request 에 path variable 이 있는경우
```java
import static org.springframework.restdocs.cookies.CookieDocumentation.cookieWithName;
import static org.springframework.restdocs.cookies.CookieDocumentation.requestCookies;
...
.andDo(document("docs_name",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
requestCookies(
cookieWithName("key").description("about")
)
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders
...
.andDo(document("docs_name",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
requestHeaders(
headerWithName("key").description("about")
)