简介
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>
添加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", "helloalanturing@icloud.com"))
.version("1.0")
.build();
}
}
测试
@RestController
@Api(description = "测试接口")
public class TestController {
@ApiOperation("测试方法")
@GetMapping("/doTest")
public String test(String param1) {
System.out.println("param1 = [" + param1 + "]");
return "TestController.test";
}
}
结果展示
启动项目,浏览器访问:http://localhost:8080/swagger-ui.html
总结
更详尽的配置,请参考官方网站!