Skip to content

Spring Restdocs Configuration

정명주(myeongju.jung) edited this page Mar 2, 2018 · 3 revisions

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
도메인개발팀 <dev@domain.com>
: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]]
== 회원

회원 관련 API

include::member/profile-get.adoc[]

resources/member/profile-get.adoc

[[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[]
Clone this wiki locally