如何利用 Swagger2Markup 生成好看的文档。
clone 项目
clone这个项目:https://github.com/Swagger2Markup/spring-swagger2markup-demo
mvn package
把demo中的pom如下位置替换成自己的api-doc地址即可。
1 2 <swaggerInput>${swagger.input}</swaggerInput> <swaggerInput>http://localhost:18081/failure/injection/v2/api-docs</swaggerInput>
输出目录:
1 2 3 4 5 6 7 8 9 10 > 额,这个输出目录不能修改,输出目录实际是asciidoc的输出,不是html和pdf的输出。 然后mvn clean test即可。 > 这种方式的 pdf 中如果有中文会乱码。https://blog.csdn.net/gongxsh00/article/details/80508963 显示请求和响应的样例JSON ```<swagger2markup.generatedExamplesEnabled>true</swagger2markup.generatedExamplesEnabled>
添加静态内容:
1 2 3 D:\temp\spring-boot\spring-swagger2markup-demo\src\docs\asciidoc manual_content1.adoc manual_content2.adoc
运行命令 1 2 3 4 @echo off cd D:\temp\spring-boot\spring-swagger2markup-demo call mvn test copy /y D:\temp\spring-boot\spring-swagger2markup-demo\target\asciidoc\html\index.html E:\workspace\code\spring-boot\FailureInjection\src\main\resources\static
asciidoc文档 index.adoc
1 2 3 4 5 include::{generated}/overview.adoc[] include::failure_type.adoc[] include::{generated}/paths.adoc[] include::{generated}/security.adoc[] include::{generated}/definitions.adoc[]
其中 failure_type.adoc 在 spring-swagger2markup-demo项目下
其他的文件都在目标项目下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 # spring boot 项目中 swagger 的配置 **FailureInjectionApplication.groovy** ```groovy @SpringBootApplication @EnableSwagger2 class FailureInjectionApplication { @Value('${swagger.host}') private String swaggerHost //@Value('${server.port}') //private String port @Bean RestTemplate restTemplate(ClientHttpRequestFactory factory) { return new RestTemplate(factory) } @Bean ClientHttpRequestFactory simpleClientHttpRequestFactory() { SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory() factory.setReadTimeout(30000)//ms factory.setConnectTimeout(60000)//ms return factory } static void main(String[] args) { SpringApplication.run(FailureInjectionApplication, args) } @Bean UiConfiguration uiConfig() { return UiConfigurationBuilder .builder() //.operationsSorter(OperationsSorter.METHOD) .operationsSorter(OperationsSorter.ALPHA) .build() } @Bean Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .host(swaggerHost) .ignoredParameterTypes(groovy.lang.MetaClass.class,java.lang.StackTraceElement.class,java.lang.Throwable.class) .select() //.apis(RequestHandlerSelectors.any()) .apis(RequestHandlerSelectors.basePackage("com.ice.appwatcher.injection.controller")) //.apis(RequestHandlerSelectors.withMethodAnnotation(ApplicationException.class)) .paths(PathSelectors.any()) //.paths(PathSelectors.ant("/api/*")) .build() } private static ApiInfo apiInfo() { return new ApiInfoBuilder() .title("应用监控-异常场景注入 API 说明文档") .description("2019.08.28 开发测试版本") //.licenseUrl("http://www.huawei.com") //.termsOfServiceUrl("http://mindao.com.cn") .contact(new Contact("iceleng", "http://appwatcher.huawei.com", "ice.lengbing@huawei.com")) .version("1.0") .build() } }