美文网首页
springboot集成swagger2导出pdf和html

springboot集成swagger2导出pdf和html

作者: 哈士奇的乐趣 | 来源:发表于2021-01-15 17:23 被阅读0次

    开发工具:idea

    首先我们需要更改maven路径

    图一

    其次更改本地maven的setting 文件

    在<mirrors> 中添加一段标签 引入阿里仓库

    <mirror> 

        <id>aliyunmaven</id>

           <mirrorOf>*</mirrorOf>

             <name>阿里云公共仓库</name> 

             <url>https://maven.aliyun.com/repository/public</url> 

    </mirror>    

    在pom.xml 中添加

    <dependency> 

        <groupId>nl.jworks.markdown_to_asciidoc</groupId> 

        <artifactId>markdown_to_asciidoc</artifactId> 

        <version>1.1</version>

        </dependency>

    <dependency>    

        <groupId>io.github.swagger2markup</groupId> 

        <artifactId>swagger2markup</artifactId> 

        <version>1.3.1</version>

    </dependency>      

    在pom.xml中的 plugins 标签下增加这段代码

    <!--此插件生成ASCIIDOC-->

    <plugin> 

    <groupId>io.github.swagger2markup</groupId> 

    <artifactId>swagger2markup-maven-plugin</artifactId> 

    <version>1.2.0</version> 

    <configuration>

     <!--此处端口一定要是当前项目启动所用的端口-->

     <swaggerInput>http://localhost:8084/</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.16</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>   

    这是我们回归项目中 maven 重新clear

    图二

    在controller中编写测试类

    @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://localhost:8098/v2/api-docs")).withConfig(config)         .build().toFolder(Paths.get("src/docs/asciidoc/generated")); 

         }   

    }  

    启动需要导出接口文档的项目

    紧接着启动测试类方法 generateAsciiDocs();

    若使用的idea,在terminal中执行:mvn asciidoctor:process-asciidoc

    执行:mvn generate-resources

    完毕!!!运行结果在src/docs目录中可以看到。。

    本人亲测有效,如有疑问可随时留言。

    相关文章

      网友评论

          本文标题:springboot集成swagger2导出pdf和html

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