SpringBoot+Swagger,生成.adoc/.md/html/pdf格式API文档

目录

1 选择Swagger版本

2 配置Swagger

3 启动项目

4 生成 .adoc 和 .md 格式文档

5 生成 html 文档

6 生成 pdf 文档

6.1 浏览器打开html文件

6.2 右键“打印”

6.3 另存为PDF​编辑

6.4 PDF文档 


1 选择Swagger版本

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
    </parent>

    <groupId>com.demo</groupId>
    <artifactId>com-test-api</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>com-test-api Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>

    </properties>

    <dependencies>
       

        <!-- swagger导出所需依赖 -->
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.9.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>io.github.swagger2markup</groupId>
            <artifactId>swagger2markup</artifactId>
            <version>1.3.3</version>
        </dependency>

       

    </dependencies>

    <build>
        <finalName>com-test-api-${project.version}</finalName>
        <plugins><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->

       

            <!--swagger静态化插件 先执行测试类生成.adoc文件再运行maven命令 asciidoctor:process-asciidoc生成html-->
            <plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>1.5.6</version>
                <configuration>
                    <sourceDirectory>./docs/asciidoc/generated</sourceDirectory>
                    <outputDirectory>./docs/asciidoc/html</outputDirectory>
                    <headerFooter>true</headerFooter>
                    <doctype>book</doctype>
                    <backend>html</backend>
                    <sourceHighlighter>coderay</sourceHighlighter>
                    <attributes>
                        <!--菜单栏在左边-->
                        <toc>left</toc>
                        <!--多标题排列-->
                        <toclevels>3</toclevels>
                        <!--自动打数字序号-->
                        <sectnums>true</sectnums>
                    </attributes>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>
        </plugins>
    </build>
</project>

2 配置Swagger

Swagger2Config.java

package com.test.config.swagger;

import com.yixin.netc.bic.im.constant.ConstantSystem;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.demo.com.test.api.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(getPars())
                .apiInfo(apiInfo());
    }


    private List<Parameter> getPars(){

        ParameterBuilder pBuiler1 = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();

        pBuiler1.name(ConstantSystem.HEADER_AUTH).description("Bearer token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
   
        pars.add(pBuiler1.build());
        return pars;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API文档")
                .description("文档描述")
                .version("V1.0.0")
                .build();
    }

}

3 启动项目

启动项目,确保Swagger API正常访问;http://localhost:9012/api/swagger-ui.html#/

4 生成 .adoc 和 .md 格式文档

新建测试类:Swagger2ConfigTest.java

执行方法generateAsciiDocs(),会在目录:/docs/asciidoc/generated生成.adoc格式文档

执行方法generateMarkdownDocs(),会在目录:/docs/markdown/generated生成.md格式文档

package com.test.config.swagger;

import io.github.swagger2markup.GroupBy;
import io.github.swagger2markup.Language;
import io.github.swagger2markup.Swagger2MarkupConfig;
import io.github.swagger2markup.Swagger2MarkupConverter;
import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder;
import io.github.swagger2markup.markup.builder.MarkupLanguage;
import org.junit.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Paths;

//@ActiveProfiles("win-debug")
public class Swagger2ConfigTest{

    /**
     * 项目运行后,运行此方法
     */
    @Test
    public void generateAsciiDocs() {
        //step1:输出Ascii格式
        try {
            //    输出Ascii格式   asciidoctor:process-asciidoc
            Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
                    .withMarkupLanguage(MarkupLanguage.ASCIIDOC)//设置生成格式
                    .withOutputLanguage(Language.ZH)//设置语言中文还是其他语言
                    .withPathsGroupedBy(GroupBy.TAGS)
                    .withGeneratedExamples()
                    .withoutInlineSchema()
                    .build();
            //设置swagger-api的json来源
            Swagger2MarkupConverter.from(new URL("http://localhost:9012/api/v2/api-docs"))
                    .withConfig(config)
                    .build()
                    .toFolder(Paths.get("./docs/asciidoc/generated"));//设置生成文件的路径
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void generateMarkdownDocs() {
        //step1:输出Ascii格式
        try {
            //    输出Ascii格式   asciidoctor:process-asciidoc
            Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
                    .withMarkupLanguage(MarkupLanguage.MARKDOWN)//设置生成格式
                    .withOutputLanguage(Language.ZH)//设置语言中文还是其他语言
                    .withPathsGroupedBy(GroupBy.TAGS)
                    .withGeneratedExamples()
                    .withoutInlineSchema()
                    .build();
            //设置swagger-api的json来源
            Swagger2MarkupConverter.from(new URL("http://localhost:9012/api/v2/api-docs"))
                    .withConfig(config)
                    .build()
                    .toFolder(Paths.get("./docs/markdown/generated"));//设置生成文件的路径
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void generateHtml() {
        // 通过插件生成html文件(根据ASCIIDOC生成html)
        // asciidoctor:process-asciidoc
    }

}

5 生成 html 文档

 通过插件生成html文件(根据ASCIIDOC生成html)

6 生成 pdf 文档

6.1 浏览器打开html文件

6.2 右键“打印”

6.3 另存为PDF

6.4 PDF文档 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡卡木樨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值