Swagger 介绍
Swagger UI 允许任何人(无论是开发团队还是最终用户)都可以可视化 API 资源并与之交互,而无需任何实现逻辑。
Swagger API 文档是根据 OpenAPI(以前称为 Swagger)规范自动生成的,可简化后端实现和客户端的使用。
Swagger 依赖
注意:Swagger 与 SpringBoot 高版本(2.6.x)不兼容(启动报错),因此需使用低版本(如 2.3.5)的 SpringBoot 。
<!-- 兼容 Swagger 的 SpringBoot 版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/>
</parent>
...
<!-- Swagger 相关依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>
Swagger 配置类(SpringBoot)
package com.example.apitestplatform.config;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Swagger 文档配置类
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket() {
ParameterBuilder builder=new ParameterBuilder();
builder.parameterType("header")
.name("token")
.description("token值")
.required(true)
.modelRef(new ModelRef("string")); // 在swagger文档里展示header
return new Docket(DocumentationType.SWAGGER_2)
.groupName("ApiDemo")
.apiInfo(apiInfo())
.globalOperationParameters(Lists.newArrayList(builder.build()))
.select() // 选择生成策略
.apis(RequestHandlerSelectors.basePackage("com.example.apitestplatform.controller")) // 选择生成文档的类(忽略该行则不做过滤)
.paths(PathSelectors.any())
.build();
}
// 定义接口文档基本信息
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("ApiDemo 系统") // 接口文档标题
.description("ApiDemo 接口文档") // 接口文档描述
.contact(new Contact("xiaoming", "", "[email protected]")) // 作者联系方式
.version("1.0") // 接口文档版本
.build();
}
}
Swagger 常用注解
- @Api(tags="API 类标题")
- @ApiOperation(value="API 方法标题")
- @ApiModel(value="实体类标题", description="实体类描述")
- @ApiModelProperty(value="实体属性描述",example="属性示例取值", required=true)
示例:
- 接口类:
package com.example.apitestplatform.controller;
import com.example.apitestplatform.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
@Api(tags="Swagger Demo 类")
@RestController
@RequestMapping(value="demo")
public class DemoController {
@ApiOperation(value="Swagger Demo get方法")
@GetMapping("loginGet")
public String loginGet() {
return "登录成功";
}
}
- User 类:
package com.example.apitestplatform.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel(value="用户类", description="用户信息")
@Data
public class User {
@ApiModelProperty(value="用户名", example="xiaoming", required=true)
private String username;
@ApiModelProperty(value="密码", example="123456", required=true)
private String password;
}