在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中导入需要的一些依赖和插件
依赖
io.github.swagger2markup
swagger2markup
1.3.3
插件:插件中需要像上面那个测试代码一样修改成你的项目IP和端口
springboot-swagger
io.github.swagger2markup
swagger2markup-maven-plugin
1.2.0
http://localhost:8080/v2/api-docs
src/docs/asciidoc/generated
ASCIIDOC
org.asciidoctor
asciidoctor-maven-plugin
1.5.3
org.asciidoctor
asciidoctorj-pdf
1.5.0-alpha.10.1
org.jruby
jruby-complete
1.7.24
src/docs/asciidoc/generated
coderay
left
output-html
generate-resources
process-asciidoc
html5
src/docs/asciidoc/html
output-pdf
generate-resources
process-asciidoc
src/docs/asciidoc/pdf
org.eclipse.m2e
lifecycle-mapping
1.0.0
org.asciidoctor
asciidoctor-maven-plugin
[1.5.3,)
process-asciidoc
最后依次执行这两条mvn指令:mvn asciidoctor:process-asciidoc 和 mvn generate-resources
然后就可以在项目的src\docs目录下看到你需要的离线文档了。