Spring Boot中使用Swagger2构建强大的RESTful API文档
Spring Boot中使用Swagger2构建强大的RESTful API文档
http://blog.didispace.com/swagger2markup-asciidoc/
添加插件
<pluginRepositories> <pluginRepository> <id>jcenter-snapshots</id> <name>jcenter</name> <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url> </pluginRepository> <pluginRepository> <snapshots> <enabled>false</enabled> </snapshots> <id>jcenter-releases</id> <name>jcenter</name> <url>http://jcenter.bintray.com</url> </pluginRepository> </pluginRepositories>
<plugin> <groupId>io.github.swagger2markup</groupId> <artifactId>swagger2markup-maven-plugin</artifactId> <version>1.3.1</version> <configuration> <swaggerInput>http://localhost:8080/v2/api-docs</swaggerInput> <!--<outputDir>src/docs/asciidoc/generated</outputDir>--> <outputFile>src/docs/asciidoc/generated/all</outputFile> <config> <swagger2markup.markupLanguage>ASCIIDOC</swagger2markup.markupLanguage> <!--<swagger2markup.markupLanguage>MARKDOWN</swagger2markup.markupLanguage>--> </config> </configuration> </plugin> <plugin> <groupId>org.asciidoctor</groupId> <artifactId>asciidoctor-maven-plugin</artifactId> <version>1.5.6</version> <configuration> <sourceDirectory>src/docs/asciidoc/generated</sourceDirectory> <outputDirectory>src/docs/asciidoc/html</outputDirectory> <backend>html</backend> <sourceHighlighter>coderay</sourceHighlighter> <attributes> <toc>left</toc> </attributes> </configuration> </plugin>
先启动应用程序,确保http://localhost:8080/v2/api-docs 可以正常访问
执行mvn命令
mvn swagger2markup:convertSwagger2markup
生产adoc文件后再生产HTML
mvn asciidoctor:process-asciidoc
查看文件
swagger2常用注解说明
一个不错的swagger ui
<!-- https://mvnrepository.com/artifact/com.github.xiaoymin/swagger-bootstrap-ui --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.8.2</version> </dependency>
修复 swaggerfox 升级 2.9.2 java.lang.NumberFormatException
生产环境中禁用
@Configuration @EnableSwagger2 public class SwaggerConfig { @Value("${swagger.show}") private boolean swaggerShow; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .enable(swaggerShow) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.hexin.messagecenter.controller.api")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("message-center APIs") .description("消息中心接口") // .termsOfServiceUrl("http://www.hxlc.com/") .version("1.0") .build(); } }
配置中添加
swagger: show: false