Spring boot 集成Swagger
文章目录
- 简介
- 环境
- 步骤
- 新建Spring boot项目
- 添加Swagger依赖
- 添加Swagger配置类
- 测试
- 结果展示
- 总结
简介
Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。本文简单介绍了在项目中集成swagger的方法。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。
环境
- Spring boot 2.0.5
- Swagger springfox-swagger-ui 2.6.1
- springfox-swagger2 2.6.1
步骤
新建Spring boot项目
添加Swagger依赖
<!--swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> 1 2 3 4 5 6 7 8 9 10 11
添加Swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.imjcker.springswagger.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口文档注释")
.description("接口文档注释说明")
.termsOfServiceUrl("帮助连接")
.contact(new Contact("imjcker", "http://imjcker.com", "[email protected]"))
.version("1.0")
.build();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24测试
@RestController
@Api(description = "测试接口")
public class TestController {
@ApiOperation("测试方法")
@GetMapping("/doTest")
public String test(String param1) {
System.out.println("param1 = [" + param1 + "]");
return "TestController.test";
}
}
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11结果展示
启动项目,浏览器访问:http://localhost:8080/swagger-ui.html

总结
更详尽的配置,请参考官方网站!
相关推荐
permanent00 2020-09-15
coolhty 2020-07-05
qingjiuquan 2020-06-07
SAMXIE 2020-11-04
XuDanT 2020-09-16
哈嘿Blog 2020-09-08
Qizonghui 2020-08-02
莫问前程 2020-08-02
SAMXIE 2020-07-26
XuDanT 2020-07-24
莫问前程 2020-07-18
Qizonghui 2020-07-18
Qizonghui 2020-06-28
Qizonghui 2020-06-25
莫问前程 2020-06-22
SAMXIE 2020-06-14
莫问前程 2020-06-14
XuDanT 2020-06-07
TimeMagician 2020-06-03