zoukankan      html  css  js  c++  java
  • 企业分布式微服务云SpringCloud SpringBoot mybatis (二十八)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是Asciidoctor格式的文件,包括request和reponse,另外其他两种httpie和curl两种流行的命令行的http请求模式。

    到目前为止,只生成了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文档了。

    adoc的书写格式,参考:http://docs.spring.io/spring-restdocs/docs/current/reference/html5/,这里不多讲解。

    需要使用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,显示如下,界面还算简洁:

    Paste_Image.png

    源码来源

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

  • 相关阅读:
    调试技术 Orisun 博客园
    Core Dump和/proc调试 Orisun 博客园
    再谈select, iocp, epoll,kqueue及各种I/O复用机制 Shallway 博客频道 CSDN.NET
    基础很重要
    Speech and Language Processing (2nd Ed.): Updates
    CS 288: Statistical Natural Language Processing
    对C++中string类型的总结
    HTTP代理实现请求报文的拦截与篡改1 jivi 博客园
    学习优秀源码 Orisun 博客园
    分享:苹果老员工可享两年离岗留职待遇
  • 原文地址:https://www.cnblogs.com/Abbie/p/8461048.html
Copyright © 2011-2022 走看看