Posts Hello Restdocs
Post
Cancel

Hello Restdocs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
plugins {
    id 'org.springframework.boot' version "3.4.4"
    id 'io.spring.dependency-management' version '1.1.4'
    id 'org.asciidoctor.jvm.convert' version '2.4.0'  // Apply the Asciidoctor plugin.
    id 'java'
}


group = 'com.zz9z9.blogcode.restdocs'
version = '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

ext {
    // Configure a snippetsDir property that defines the output location for generated snippets.
    snippetsDir = file('build/generated-snippets')
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    implementation 'org.projectlombok:lombok:1.18.30'
    annotationProcessor 'org.projectlombok:lombok:1.18.30'

    testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
}

test {
    // Make Gradle aware that running the test task will write output to the snippetsDir
    outputs.dir snippetsDir
    useJUnitPlatform()
}

// Configure the asciidoctor task.
asciidoctor {
    inputs.dir snippetsDir  // Make Gradle aware that running the task will read input from the snippetsDir.
    attributes 'snippets': snippetsDir
    dependsOn test
}

bootJar {
    dependsOn asciidoctor
    from("${asciidoctor.outputDir}") {
        into 'static/docs'
    }
}
1
2
3
4
5
6
7
8
9
10
@RestController
@RequestMapping("/api")
public class HelloController {

    @GetMapping("/hello")
    public Map<String, String> hello() {
        return Map.of("message", "Hello, Spring REST Docs!");
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@AutoConfigureMockMvc
@AutoConfigureRestDocs(outputDir = "build/generated-snippets")
@SpringBootTest
public class HelloControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void helloRestDocs() throws Exception {
        mockMvc.perform(get("/api/hello"))
                .andExpect(status().isOk())
                .andDo(document("hello",
                        responseFields(
                                fieldWithPath("message").description("The hello message")
                        )
                ));
    }

}

참고 자료

  • https://docs.spring.io/spring-restdocs/docs/current/reference/htmlsingle/
This post is licensed under CC BY 4.0 by the author.

API 스펙을 제공할 때 생각해볼 것들

Hello Spring Mvc Test