目录
之前做项目的使用,由于是前后端分离开发,后端开发完成以后需要和前端联调,使用swagger可以保证接口和项目中的代码是最新的,下面介绍swagger的使用111
- 在pom文件引入相关的依赖
<!--swagger2的jar包--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!--引入视觉的样式的UI--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
- swagger配置文件编写
package com.example.start.swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
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.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @Author luckylittle
* @Date 10:11 2020/12/6
* @Description 通过 @Configuration 注解,让 Spring 加载该配置类。再通过 @EnableSwagger2 注解来启用Swagger2
*/
@EnableSwagger2
@Configuration
public class SwaggerConfig {
/**
* apiInfo() 用来创建该 Api 的基本信息(这些基本信息会展现在文档页面中)
*
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("利用swagger构建api文档")
.description("大宝剑项目web服务接口")
.termsOfServiceUrl("http://www.bigsword.com")
.version("1.1.0")
.contact(new Contact("luckylittle", "http://www.bigsword.com", "luckylittle@qq.com"))
.build();
}
/**
* createRestApi 函数创建 Docket 的Bean
*
* @param
* @return
* @Author luckylittle
* @Date 10:33 2020/12/6
*/
@Bean
public Docket createRestApi() {
List<ResponseMessage> responseMessageList = new ArrayList<>();
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)//Swagger 不使用默认的 HTTP 响应消息
.apiInfo(apiInfo())//创建API的基本信息
.produces(Collections.singleton("application/json"))
.consumes(Collections.singleton("application/json"))
.protocols(Collections.singleton("http"))
.select()//select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现
.apis(RequestHandlerSelectors.basePackage("com.example.start"))//指定扫描的包路径
.paths(PathSelectors.any())//Swagger会扫描该包下的所有Controller定义的API,并产生文档内容(除了被@ApiIgnore定义的请求)
.build()
.globalOperationParameters(operationParameters())//添加默认的参数
.globalResponseMessage(RequestMethod.GET, responseMessageList)//通过 Docket 的 globalResponseMessage()
// 方法全局覆盖 HTTP 方法的响应消息
.globalResponseMessage(RequestMethod.POST, responseMessageList);
}
/**
* 创建固定的请求头
*
* @param
* @return
* @Author luckylittle
* @Date 10:29 2020/12/6
*/
private List<Parameter> operationParameters() {
// 统一在请求头中增加token
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
pars.add(tokenPar
.name("Token")
.description("Token")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build());
pars.add(tokenPar
.name("userId")
.description("userId")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build());
pars.add(tokenPar.build());
return pars;
}
}
- controller中代码实例
package com.example.start.swagger;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
/**
* @Author luckylittle
* @Date 10:44 2020/12/6
* @Description
*/
@RestController
@RequestMapping("/swagger")
@Api(value = "使用swagger的用例")
public class SwaggerControllerTest {
@PostMapping("/add")
@ApiOperation("add teacher info to db")
public String add(@RequestBody Teacher teacher) {
//调用添加老师的接口
return "添加成功";
}
@GetMapping("/getOne")
@ApiOperation("select teacher info from db")
public String getTeacher(String id) {
//调用添查询师的接口
return "查询成功";
}
}
- 访问http://localhost:8088/swagger-ui.html# 查看api文档页面,调试接口
- 访问http://localhost:8088/v2/api-docs导出swagger接口,项目发布会需要这个
- 下面是Swagger常用注解的使用
- @Api:修饰整个类,描述Controller的作用
- @ApiOperation:描述一个类的一个方法,或者说一个接口
- @ApiParam:单个参数描述
- @ApiModel:用对象来接收参数
- @ApiProperty:用对象接收参数时,描述对象的一个字段
- @ApiResponse:HTTP响应其中1个描述
- @ApiResponses:HTTP响应整体描述
- @ApiIgnore:使用该注解忽略这个API
- @ApiError :发生错误返回的信息
- @ApiImplicitParam:描述一个请求参数,可以配置参数的中文含义,还可以给参数设置默认值
- @ApiImplicitParams:描述由多个 @ApiImplicitParam 注解的参数组成的请求参数列表