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[]