Spring Restdocs Configuration - redutan/redutan.github.io GitHub Wiki
gradle
TODO
spring-bootλ₯Ό μ΄μ©ν μ
RestDocsMockMvcConfigurationCustomizer
λ₯Ό μ΄μ©ν κ°νΈν μ€μ μ΄ κ°λ₯ν κ² κ°λ€. μ΄κ²λ μλ!!
springRestdocs.gradle
apply plugin: "org.asciidoctor.convert"
ext['snippetsDir'] = file('build/generated-snippets')
ext['spring-restdocs.version'] = '1.2.3.RELEASE'
dependencies {
testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc'
asciidoctor "org.springframework.restdocs:spring-restdocs-asciidoctor:${project.ext['spring-restdocs.version']}"
}
//noinspection GroovyAssignabilityCheck
test {
//noinspection GroovyAssignabilityCheck
systemProperties.put("spring.profiles.active", System.properties.get("spring.profiles.active", "local"))
outputs.dir snippetsDir
}
asciidoctor {
//noinspection GroovyAssignabilityCheck
attributes 'snippets': snippetsDir
inputs.dir snippetsDir
dependsOn test
}
jar {
dependsOn asciidoctor
from ("${asciidoctor.outputDir}/html5") {
into 'static/docs'
}
}
build.gradle
buildscript {
ext {
asciidoctorPluginVersion = '1.5.6'
}
repositories {
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "org.asciidoctor:asciidoctor-gradle-plugin:${asciidoctorPluginVersion}"
}
}
Test
MemberRestControllerTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class MemberRestControllerTest {
@Rule
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
@Autowired
private WebApplicationContext context;
@Autowired
private RestDocsProperties restDocsProperties;
private RestDocumentationResultHandler documentationHandler;
private MockMvc mockMvc;
@Before
public void setUp() {
this.documentationHandler = document("member/{method-name}",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()));
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)
.uris()
.withScheme(restDocsProperties.getSchema())
.withHost(restDocsProperties.getHost())
.withPort(restDocsProperties.getPort()))
.apply(springSecurity())
.alwaysDo(this.documentationHandler)
.build();
}
@Test
public void profile() throws Exception {
mockMvc.perform(get("/api/members/profile")
.accept(MediaType.APPLICATION_JSON)
.cookie(new Cookie("X-TOKEN", TOKEN)))
.andExpect(status().isOk())
.andDo(this.documentationHandler.document(
responseFields(
fieldWithPath("memberId").description("νμμμ΄λ"),
fieldWithPath("nickname").description("λλ€μ"),
fieldWithPath("thumbnailUrl").description("νμμΈλ€μΌμ΄λ―Έμ§ Url").type(STRING).optional(),
fieldWithPath("available").description("μ¬μ©κ°λ₯ μ¬λΆ"),
fieldWithPath("curator").description("νλ μ΄ν° μ¬λΆ"))
)
)
;
}
}
asciidoc
V1_0.adoc
= λλ©μΈ API Document v1.0
λλ©μΈκ°λ°ν <[email protected]>
:doctype: book
:icons: font
:source-highlighter: highlightjs
:toc: right
:toclevels: 2
:sectlinks:
//:operation-curl-request-title: Example request
//:operation-http-response-title: Example response
include::resources/member.adoc[]
resources/member.adoc
[member](/redutan/redutan.github.io/wiki/member)
== νμ
νμ κ΄λ ¨ API
include::member/profile-get.adoc[]
resources/member/profile-get.adoc
[member-profile-get](/redutan/redutan.github.io/wiki/member-profile-get)
=== νμ νλ‘ν μ‘°ν
`/api/members/profile/` *[GET]*
include::{snippets}/member/profile/http-request.adoc[]
==== Request
==== Response
*200*
===== fields
include::{snippets}/member/profile/response-fields.adoc[]
===== Client Errors
|===
|Code|Description
| `401` | μΈμ¦(λ‘κ·ΈμΈ)λμ§ μμ κ²½μ°
|===
==== Example
include::{snippets}/member/profile/curl-request.adoc[]
include::{snippets}/member/profile/http-response.adoc[]