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

相关推荐
fa_lsyk24 分钟前
maven环境搭建
java·maven
m0_743048444 小时前
Spring MVC练习
java·spring·mvc
小蒜学长7 小时前
校园周边美食探索及分享平台
java·spring boot·后端·spring·apache·美食
Wx-bishekaifayuan8 小时前
PHP动物收容所管理系统-计算机设计毕业源码94164
java·css·spring boot·spring·spring cloud·servlet·php
古城小栈11 小时前
Spring Security 认证流程,长话简说
java·python·spring
wmxz52013 小时前
SpringMVC处理请求流程
java·spring boot·后端·spring·java-ee
tian-ming18 小时前
JavaWeb后端开发案例——苍穹外卖day01
spring boot·spring
xybm160518 小时前
MTSET可溶于DMSO、DMF、THF等有机溶剂,并在水中有轻微的溶解性,91774-25-3
java·spring·eclipse·tomcat·1024程序员节
dream_ready18 小时前
四万字长文SpringBoot、Spring、SpringMVC等相关面试题(注:该篇博客将会持续维护 最新维护时间:2024年11月12日)
java·spring boot·后端·spring·面试·1024程序员节
张铁铁是个小胖子19 小时前
显示微服务间feign调用的日志
java·spring·微服务