
Swagger是使用OpenAPI规范(OAS)开发API的最广泛使用的工具生态系统。Swagger由开源和专业工具组成,满足几乎所有的需求和用例。
一、配置Swagger 添加依赖// web依赖添加配置类//swagger依赖 org.springframework.boot spring-boot-starter-web io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi()
{
return new Docket(documentationType.SWAGGER_2)
.groupName("")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("包名"))
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo()
{
return new ApiInfoBuilder()
.title("接口")
.description("接口说明")
.version("1.0")
.build();
}
}
使用
// 控制层
@Api(tags = "基础模块")
@RestController
@RequestMapping("/base")
public class baseController {
@ApiOperation(value = "查询")
@RequestMapping(value = "/findList", method = RequestMethod.POST)
public RestResponse findList(@RequestBody Param param)
{
return RestResponse.ok();
}
}
访问地址
localhost:8080/swagger-ui.html
将接口文档暴露在外网会出现一定的安全问题,此时我们需要给Swagger文档配置登录密码。
二、配置Swagger登录密码 添加依赖更新配置类com.github.xiaoymin swagger-bootstrap-ui 1.9.3
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI //添加注解
public class SwaggerConfig {
}
添加启动类注解
@EnableSwagger2
配置yaml文件swagger:
basic:
enable: true // 启用
username: 用户名
password: 密码