概述
Swagger™ 的目标是为 REST API 定义一个标准的、与语言无关的接口,它允许人们和计算机在不访问源代码、文档或通过网络流量检查的情况下发现和理解服务的功能。当通过 Swagger 正确定义,用户可以使用最少的实现逻辑理解远程服务并与之交互。
Swagger注解
swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。
- @Api:修饰整个类,描述Controller的作用
- @ApiOperation:描述一个类的一个方法,或者说一个接口
- @ApiParam:单个参数描述
- @ApiModel:用对象来接收参数
- @ApiProperty:用对象接收参数时,描述对象的一个字段
- @ApiResponse:HTTP响应其中1个描述
- @ApiResponses:HTTP响应整体描述
- @ApiIgnore:使用该注解忽略这个API
- @ApiError :发生错误返回的信息
- @ApiImplicitParam:一个请求参数
- @ApiImplicitParams:多个请求参数
整合Swagger3
添加 Maven 依赖
<!-- springfox -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
增加Swagger
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger3")
.description("Swagger3")
.contact(new Contact("zxk", null, "abc@123.com"))
.version("1.0")
.build();
}
}
查看Swagger
访问:http://127.0.0.1:8083/swagger-ui/index.html#/
参考
https://swagger.io/tools/open-source/getting-started/
https://docs.swagger.io/
https://petstore.swagger.io/#/pet/uploadFile
https://github.com/swagger-api/swagger-ui/