1.确保本地系统http://localhost:8080/swagger-ui.html及http://localhost:8080/v2/api-docs可以访问正常访问
如下图
2.在pom.xml中增加maven依赖及插件
<dependency> <groupId>io.github.swagger2markup</groupId> <artifactId>swagger2markup</artifactId> <version>1.3.1</version> </dependency>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.1.1.RELEASE</version> <configuration> <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 --> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.0.0</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> <warName>${project.artifactId}</warName> </configuration> </plugin> <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/all</outputDir> <config> <!-- 除了ASCIIDOC之外,还有MARKDOWN和CONFLUENCE_MARKUP可选 --> <swagger2markup.markupLanguage>ASCIIDOC</swagger2markup.markupLanguage> </config> </configuration> </plugin> <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.21</version> </dependency> </dependencies> <!-- Configure generic document generation settings --> <configuration> <sourceDirectory>src/docs/asciidoc/generated/all</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> </build>
3.项目src下建src/docs/asciidoc/generated/all文件目录结构
4.在项目中创建下面的类
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.URL; import java.nio.file.Paths; /** * @Author jack * @Date 2020/02/29 23:11 */ //@RunWith(SpringRunner.class) //@RunWith(SpringJUnit4ClassRunner.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:8080/v2/api-docs")) .withConfig(config) .build() .toFolder(Paths.get("src/docs/asciidoc/generated/all")); } }
5.启动项目测试http://localhost:8080/swagger-ui.html及http://localhost:8080/v2/api-docs可以访问正常访问是否可以正常访问,若可以正常访问进行下一步(保持系统运行)
6.运行第四步创建的类中@Test方法
若此步骤测试运行报空指针,就查看那个api返回的ModelAndView是空。其他错误考虑jar包冲突问题。
正确运行后获得一下文件
7.运行maven命令如下图,自动生成html和pdf文件夹及文件说明pdf和html文件生成成功
8.想要word文件的自己百度pdf如何转word吧,在此不再赘述
注:若依系统测试pdf/html自动生成成功,若有问题欢迎大家指出