smart-doc 生成接口文档

使用smart-doc 生成接口文档

  方式一  插件方式    springboot 启动类 运行生成接口文档

1.pom文件 导入插件

 <!--smart-doc 生成接口文档-->
            <plugin>
                <groupId>com.github.shalousun</groupId>
                <artifactId>smart-doc-maven-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <!--指定生成文档的使用的配置文件,配置文件放在自己的项目中-->
                    <configFile>./src/main/resources/smart-doc.json</configFile>
                    <!--指定项目名称-->
                    <projectName>测试</projectName>
                    <!--smart-doc实现自动分析依赖树加载第三方依赖的源码,如果一些框架依赖库加载不到导致报错,这时请使用excludes排除掉-->
                    <excludes>
                        <!--格式为:groupId:artifactId;参考如下-->
                        <exclude>com.alibaba:fastjson</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <!--如果不需要在执行编译时启动smart-doc,则将phase注释掉-->
                        <phase>compile</phase>
                        <goals>
                            <goal>html</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

2.resource目录下  smart-doc.json 配置文件

{
  "serverUrl": "http://127.0.0.1",//服务器地址
  "isStrict": false,//是否用严格模式,严格模式强制检查注释
  "allInOne": true,//所有接口文档合并生成到一个文档
  "outPath": "src/main/resources/static/doc",//文档的输出路径
  "projectName": "smart-doc"//指定项目名称,用于显示在文档中
}

 方式二  jar包方式

1.pom 导入jar包

        <!--smart-doc 生成接口文档-->
        <dependency>
            <groupId>com.github.shalousun</groupId>
            <artifactId>smart-doc</artifactId>
            <version>2.2.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

2.junit Test 运行 运行生成接口文档

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = IcbcServerAppApplication.class )
public class ApiTest {

    /**
     * 包括设置请求头,缺失注释的字段批量在文档生成期使用定义好的注释
     */
    @Test
    public void testBuilderControllersApi() {
        ApiConfig config = new ApiConfig();
        config.setServerUrl("192.168.2.77:8082/");
        //true会将文档合并导出到一个markdown
        config.setAllInOne(true);
        //生成html时加密文档名不暴露controller的名称
        config.setMd5EncryptedHtmlName(true);


        //指定文档输出路径
        //@since 1.7 版本开始,选择生成静态html doc文档可使用该路径:DocGlobalConstants.HTML_DOC_OUT_PATH;
      //config.setOutPath(DocGlobalConstants.HTML_DOC_OUT_PATH);
        // @since 1.2,如果不配置该选项,则默认匹配全部的controller,
        // 如果需要配置有多个controller可以使用逗号隔开
        config.setPackageFilters("com.jdd.icbc.client.rest.IcbcUserAccountAutoRechargeConrtroller");
        //不指定SourcePaths默认加载代码为项目src/main/java下的,如果项目的某一些实体来自外部代码可以一起加载
//        config.setSourceCodePaths(
//                //自1.7.0版本开始,在此处可以不设置本地代码路径,单独添加外部代码路径即可
//            SourceCodePath.path().setDesc("本项目代码").setPath("src/main/java"),
//                SourceCodePath.path().setDesc("加载项目外代码").setPath("E:\ApplicationPower\ApplicationPower\Common-util\src\main\java")
//        );
        //since 1.7.5
        //如果该选项的值为false,则smart-doc生成allInOne.md文件的名称会自动添加版本号
        config.setCoverOld(true);
        //since 1.7.5
        //设置项目名(非必须),如果不设置会导致在使用一些自动添加标题序号的工具显示的序号不正常
        config.setProjectName("icbc-server-app");


//
        //非必须只有当setAllInOne设置为true时文档变更记录才生效,https://gitee.com/sunyurepository/ApplicationPower/issues/IPS4O
        config.setRevisionLogs(
                RevisionLog.builder().setRevisionTime("2021/08/06").setAuthor("zhangweifeng").setRemarks("自动充值接口文档测试").setStatus("创建").setVersion("V1.0")
//                RevisionLog.getLog().setRevisionTime("2018/12/16").setAuthor("chen2").setRemarks("测试2").setStatus("修改").setVersion("V2.0")
        );

        ApiDocBuilder.buildApiDoc(config);

        //@since 1.7+版本开始,smart-doc支持生成带书签的html文档,html文档可选择下面额方式
        //HtmlApiDocBuilder.builderControllersApi(config);
        //@since 1.7+版本开始,smart-doc支撑生成AsciiDoc文档,你可以把AsciiDoc转成HTML5的格式。
        //@see https://gitee.com/sunyurepository/api-doc-test
        //AdocDocBuilder.builderControllersApi(config);

        System.out.println("生成接口文档完成");
    }
}
古人学问无遗力,少壮工夫老始成。 纸上得来终觉浅,绝知此事要躬行。
原文地址:https://www.cnblogs.com/wf-zhang/p/15107578.html