Spring Boot中使用Swagger2构建强大的RESTful API文档
Posted ejinxian
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot中使用Swagger2构建强大的RESTful API文档相关的知识,希望对你有一定的参考价值。
Spring Boot能够快速开发、便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API。
添加Swagger2依赖
在pom.xml
中加入Swagger2的依赖
io.springfoxspringfox-swagger22.6.1io.springfoxspringfox-swagger-ui2.6.1
创建Swagger2配置类
在创建Swagger2的配置类Swagger2
。
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.ec.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("简单优雅的restful api 风格")
.description("简单优雅的restful api 风格,http://localhost:8080/ecoss/swagger-ui.html#/")
.termsOfServiceUrl("http://localhost:8080/ecoss")
.version("1.0")
.build();
}
}
如上代码所示,通过
@Configuration
注解,让Spring来加载该类配置。再通过
@EnableSwagger2
注解来启用Swagger2。
创建Controller
以上是关于Spring Boot中使用Swagger2构建强大的RESTful API文档的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot中使用Swagger2构建RESTful APIS(含源码)
Spring Boot中使用Swagger2构建强大的RESTful API文档