美文网首页
SringBoot+Swagger2导出pdf离线文档

SringBoot+Swagger2导出pdf离线文档

作者: 不需要什么就能清醒 | 来源:发表于2020-01-09 18:03 被阅读0次

在SpringBoot项目中使用了Swagger2可以使用以下办法导出离线文件
首先将下面代码拷贝到你的项目中测试运行(必须要先运行这个测试类)
注意:要修改成你的URL地址

@RunWith(SpringRunner.class)
public class ExportConfig {

    @Test
    public void generateAsciiDocs() throws Exception {

        // 输出Ascii格式
        Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder().withMarkupLanguage(MarkupLanguage.ASCIIDOC)
                .withOutputLanguage(Language.ZH).withPathsGroupedBy(GroupBy.TAGS).withGeneratedExamples()
                .withoutInlineSchema().build();

        Swagger2MarkupConverter.from(new URL("http://172.21.3.15:8080/v2/api-docs")).withConfig(config)
                .build().toFolder(Paths.get("src/docs/asciidoc/generated"));
    }
}

然后在pom.xml中导入需要的一些依赖和插件
依赖

    <!-- ********************* swagger导出PDF/HTML所需依赖 START ********************************* -->
    <dependency>
        <groupId>io.github.swagger2markup</groupId>
        <artifactId>swagger2markup</artifactId>
        <version>1.3.3</version>
    </dependency>
    <!-- ********************* swagger导出PDF/HTML所需依赖 END ********************************* -->

插件:插件中需要像上面那个测试代码一样修改成你的项目IP和端口

<build>
    <finalName>springboot-swagger</finalName>
    <plugins>
        <!--此插件生成ASCIIDOC-->
            <plugin>
                <groupId>io.github.swagger2markup</groupId>
                <artifactId>swagger2markup-maven-plugin</artifactId>
                <version>1.2.0</version>
                <configuration>
                    <!--此处端口一定要是当前项目启动所用的端口-->
                    <swaggerInput>http://localhost:8080/v2/api-docs</swaggerInput>
                    <outputDir>src/docs/asciidoc/generated</outputDir>
                    <config>
                        <!-- 除了ASCIIDOC之外,还有MARKDOWN和CONFLUENCE_MARKUP可选 -->
                        <swagger2markup.markupLanguage>ASCIIDOC</swagger2markup.markupLanguage>
                    </config>
                </configuration>
            </plugin>
            <!--此插件生成HTML和PDF-->
            <plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>1.5.3</version>
                <!-- Include Asciidoctor PDF for pdf generation -->
                <dependencies>
                    <dependency>
                        <groupId>org.asciidoctor</groupId>
                        <artifactId>asciidoctorj-pdf</artifactId>
                        <version>1.5.0-alpha.10.1</version>
                    </dependency>
                    <dependency>
                        <groupId>org.jruby</groupId>
                        <artifactId>jruby-complete</artifactId>
                        <version>1.7.24</version>
                    </dependency>
                </dependencies>
                <!-- Configure generic document generation settings -->
                <configuration>
                    <sourceDirectory>src/docs/asciidoc/generated</sourceDirectory>
                    <sourceHighlighter>coderay</sourceHighlighter>
                    <attributes>
                        <toc>left</toc>
                    </attributes>
                </configuration>
                <!-- Since each execution can only handle one backend, run
                     separate executions for each desired output type -->
                <executions>
                    <execution>
                        <id>output-html</id>
                         <phase>generate-resources</phase>
                         <goals>
                             <goal>process-asciidoc</goal>
                         </goals>
                         <configuration>
                             <backend>html5</backend>
                             <outputDirectory>src/docs/asciidoc/html</outputDirectory>
                         </configuration>
                    </execution>
                    <execution>
                        <id>output-pdf</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                        <configuration>
                            <backend>pdf</backend>
                            <outputDirectory>src/docs/asciidoc/pdf</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.asciidoctor
                                        </groupId>
                                        <artifactId>
                                            asciidoctor-maven-plugin
                                        </artifactId>
                                        <versionRange>
                                            [1.5.3,)
                                        </versionRange>
                                        <goals>
                                            <goal>
                                                process-asciidoc
                                            </goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
  </build>

最后依次执行这两条mvn指令:mvn asciidoctor:process-asciidoc 和 mvn generate-resources
然后就可以在项目的src\docs目录下看到你需要的离线文档了。

相关文章

网友评论

      本文标题:SringBoot+Swagger2导出pdf离线文档

      本文链接:https://www.haomeiwen.com/subject/taxuactx.html