您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页SpringBoot非官方教程 | 第十篇: 用Spring

SpringBoot非官方教程 | 第十篇: 用Spring

来源:化拓教育网

准备工作

你需要15min
Jdk 1.8
maven 3.0+
idea

创建工程

引入依赖,其pom文件:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.restdocs</groupId>
        <artifactId>spring-restdocs-mockmvc</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

通过@SpringBootApplication,开启springboot

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

在springboot通常创建一个controller:

@RestController
public class HomeController {

    @GetMapping("/")
    public Map<String, Object> greeting() {
        return Collections.singletonMap("message", "Hello World");
    }

}

启动工程,访问localhost:8080,浏览器显示:

{“message”:”Hello World”}

证明接口已经写好了,但是如何通过restdoc生存api文档呢

Restdoc,通过单元测试生成api文档

restdocs是通过单元测试生存snippets文件,然后snippets根据插件生成htm文档的。

建一个单元测试类:

@RunWith(SpringRunner.class)
@WebMvcTest(HomeController.class)
@AutoConfigureRestDocs(outputDir = "target/snippets")
public class WebLayerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void shouldReturnDefaultMessage() throws Exception {
        this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
                .andExpect(content().string(containsString("Hello World")))
                .andDo(document("home"));
    }
}

其中,@ AutoConfigureRestDocs注解开启了生成snippets文件,并指定了存放位置。

启动单元测试,测试通过,你会发现在target文件下生成了一个snippets文件夹,其目录结构如下:

└── target
    └── snippets
        └── home
            └── httpie-request.adoc
            └── curl-request.adoc
            └── http-request.adoc
            └── http-response.adoc

到目前为止,只生成了Snippets文件,需要用Snippets文件生成文档。

怎么用Snippets

创建一个新文件src/main/asciidoc/index.adoc :

用 Spring REST Docs 构建文档

This is an example output for a service running at http://localhost:8080:

.request
include::{snippets}/home/http-request.adoc[]

.response
include::{snippets}/home/http-response.adoc[]

这个例子非常简单,通过单元测试和一些简单的配置就能够得到api文档了。

需要使用asciidoctor-maven-plugin插件,在其pom文件加上:

<plugin>
    <groupId>org.asciidoctor</groupId>
    <artifactId>asciidoctor-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>generate-docs</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>process-asciidoc</goal>
            </goals>
            <configuration>
                <sourceDocumentName>index.adoc</sourceDocumentName>
                <backend>html</backend>
                <attributes>
                    <snippets>${project.build.directory}/snippets</snippets>
                </attributes>
            </configuration>
        </execution>
    </executions>
</plugin>

这时只需要通过mvnw package命令就可以生成文档了。
在/target/generated-docs下有个index.html,打开这个html,显示如下,界面还算简洁:

图片.png

结语

通过单元测试,生存adoc文件,再用adoc文件生存html,只需要简单的几步就可以生成一个api文档的html文件,这个html文件你可以通网站发布出去。整个过程很简单,对代码无任何影响。

参考资料

写在最后

欢迎关注喜欢、和点赞后续将推出更多的spring cloud教程,敬请期待。
欢迎关注我的微信公众号获取更多更全的学习资源,视频资料,技术干货!

欢迎扫码关注

公众号回复“学习”,拉你进程序员技术讨论群。

公众号回复“视频”,领取800GJava视频学习资源。

java学习全套
820G资源

公众号回复“领取资源”,领取1T前端Java产品经理微信小程序Python等资源合集大放送。

全栈资料
接近1T资源

公众号回复“慕课”,领取1T慕课实战学习资源。

慕课实战大全
1061G资源

公众号回复“实战”,领取750G项目实战学习资源。

前后端实战项目
750实战资源

公众号回复“面试”,领取8G面试实战学习资源。

JAVA面试实战视频
8G面试资源

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务