Swagger文档转html和pdf格式_亲测成功

Swagger文档转html和pdf格式_亲测成功

spring maven项目 使用Swagger作为接口文档时,可以导出html和pdf格式.

maven项目pom.xml引入
java 复制代码
	     <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>

		<asciidoctor.input.directory>${project.basedir}/src/docs2/asciidoc</asciidoctor.input.directory>
		<generated.asciidoc.directory>${project.build.directory}/asciidoc</generated.asciidoc.directory>
		<asciidoctor.html.output.directory>${project.build.directory}/asciidoc/html</asciidoctor.html.output.directory>
		<asciidoctor.pdf.output.directory>${project.build.directory}/asciidoc/pdf</asciidoctor.pdf.output.directory>
	     </properties>


		<!--离线文档-->
		<dependency>
			<groupId>org.springframework.restdocs</groupId>
			<artifactId>spring-restdocs-mockmvc</artifactId>
			<scope>test</scope>
		</dependency>
		<!--生成静态文档-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-staticdocs</artifactId>
			<version>2.4.0</version>
		</dependency>

		<!--通过Asciidoctor使得asciidoc生成其他的文档格式,例如:PDF 或者HTML5-->
		<plugin>
			<groupId>org.asciidoctor</groupId>
			<artifactId>asciidoctor-maven-plugin</artifactId>
			<version>1.5.3</version>
			<!-- Include Asciidoctor PDF for pdf generation -->
			<!--生成PDF-->
			<dependencies>
				<dependency>
					<groupId>org.asciidoctor</groupId>
					<artifactId>asciidoctorj-pdf</artifactId>
					<version>1.5.0-alpha.14</version>
				</dependency>
				<!-- Comment this section to use the default jruby artifact provided by the plugin -->
				<dependency>
					<groupId>org.jruby</groupId>
					<artifactId>jruby-complete</artifactId>
					<version>1.7.21</version>
				</dependency>
			</dependencies>

			<!-- Configure generic document generation settings -->
			<!--文档生成配置-->
			<configuration>
				<sourceDirectory>${asciidoctor.input.directory}</sourceDirectory>
				<sourceDocumentName>index.adoc</sourceDocumentName>
				<attributes>
					<doctype>book</doctype>
					<toc>left</toc>
					<toclevels>3</toclevels>
					<numbered></numbered>
					<hardbreaks></hardbreaks>
					<sectlinks></sectlinks>
					<sectanchors></sectanchors>
					<generated>${generated.asciidoc.directory}</generated>
				</attributes>
			</configuration>
			<!-- Since each execution can only handle one backend, run
               			separate executions for each desired output type -->
			<!--因为每次执行只能处理一个后端,所以对于每个想要的输出类型,都是独立分开执行-->
			<executions>
				<!--html5-->
				<execution>
					<id>output-html</id>
					<phase>test</phase>
					<goals>
						<goal>process-asciidoc</goal>
					</goals>
					<configuration>
						<backend>html5</backend>
						<outputDirectory>${asciidoctor.html.output.directory}</outputDirectory>
					</configuration>
				</execution>
				<!--pdf-->
				<execution>
					<id>output-pdf</id>
					<phase>test</phase>
					<goals>
						<goal>process-asciidoc</goal>
					</goals>
					<configuration>
						<backend>pdf</backend>
						<outputDirectory>${asciidoctor.pdf.output.directory}</outputDirectory>
					</configuration>
				</execution>
			</executions>
		</plugin>
根据 swagger2 生成文档
java 复制代码
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class Swagger2Test {

        /**
         * 生成AsciiDocs格式文档
         */
        @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:8080/server/v2/api-docs"))
                    .withConfig(config)
                    .build()
                    .toFolder(Paths.get("src/docs/asciidoc/generated"));
        }

	/**
         * 生成AsciiDocs格式文档,并汇总成一个文件
         */
        @Test
        public void generateAsciiDocsToFile() 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:8080/server/v2/api-docs"))
                    .withConfig(config)
                    .build()
                    .toFile(Paths.get("src/docs/asciidoc/generated/all"));
        }

}
把文档转成html和pdf文档
java 复制代码
maven命令 clean test -Dmaven.test.skip=true

inner-verify-server项目中使用到Swagger文档转html和pdf功能

参考链接:

成功了转pdf和html

https://blog.csdn.net/fly910905/article/details/79131755

https://www.leftso.com/blog/402.html

http://www.spring4all.com/article/699

https://www.cnblogs.com/yanqin/p/9145941.html

https://blog.csdn.net/samt007/article/details/79846966

https://www.cnblogs.com/sjshare/p/9350135.html

相关推荐
wxin_VXbishe6 分钟前
C#(asp.net)学员竞赛信息管理系统-计算机毕业设计源码28790
java·vue.js·spring boot·spring·django·c#·php
无心水14 分钟前
分布式定时任务与SELECT FOR UPDATE:从致命陷阱到优雅解决方案(实战案例+架构演进)
服务器·人工智能·分布式·后端·spring·架构·wpf
Coder_Boy_1 小时前
Deeplearning4j+ Spring Boot 电商用户复购预测案例
java·人工智能·spring boot·后端·spring
闻哥1 小时前
Kafka高吞吐量核心揭秘:四大技术架构深度解析
java·jvm·面试·kafka·rabbitmq·springboot
千寻技术帮1 小时前
10327_基于SpringBoot的视频剪辑咨询网站
mysql·源码·springboot·代码·视频咨询
历程里程碑1 小时前
普通数组----轮转数组
java·数据结构·c++·算法·spring·leetcode·eclipse
callJJ2 小时前
Spring AI ImageModel 完全指南:用 OpenAI DALL-E 生成图像
大数据·人工智能·spring·openai·springai·图像模型
郝学胜-神的一滴2 小时前
超越Spring的Summer(一): PackageScanner 类实现原理详解
java·服务器·开发语言·后端·spring·软件构建
MX_935915 小时前
Spring的bean工厂后处理器和Bean后处理器
java·后端·spring
程序员泠零澪回家种桔子16 小时前
Spring AI框架全方位详解
java·人工智能·后端·spring·ai·架构