1、创建好spring boot项目以后添加swagger依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-annotations -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-models -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-models</artifactId>
<version>2.1.4</version>
</dependency>
2、创建swaggerConfig类
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// 返回ApiSelectorBuilder实例,用来控制对那些接口进行展现
.select()
// 扫描所有有注解的api,用这种方式更灵活
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// 扫描指定包中的swagger注解
// .apis(RequestHandlerSelectors.basePackage("com.test.swagger.controller"))
// 扫描所有 .apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
// 设置标题
.title("测试swagger2")
// 描述
.description("模拟创建开发是使用的swagger接口")
.termsOfServiceUrl("url")
// 版本
.version("1.0")
// 作者信息
.contact(new Contact("思念","https://blog.csdn.net/weixin_43978032/article/details/111168239","邮箱"))
.build();
}
}
3
@Api 标注在类上,对类进行说明
@ApiOperation 标注在方法上,对方法进行说明
@ApiImplicitParams 标注在方法上,对方法的多个参数进行说明
@ApiImplicitParam 标注在方法上,对方法的一个参数进行说明
@ApiModel 标注在模型Model上,对模型进行说明
@ApiModelProperty 标注在属性上,对模型的属性进行说明
@ApiIgnore 标注在类或方法上,表示忽略这个类或方法
@Api():注解类,描述Controller的作用
tags = ”描述Controller的作用“
@ApiOperation():注解方法,描述具体接口的作用
value=“具体说明接口的作用”
notes="接口方法备注“
@ApiImplicitParams():注解接口方法,描述一组请求参数,可以包含多个@ApiImplicitParam()
@ApiImplicitParam():注解接口方法,描述一个具体的请求参数
name:请求参数名
value:请求参数描述
required:是否必传
dataType:参数类型
defaultValue:默认值
@ApiResponses():注解接口方法,描述一组HTTP返回值,可以包含多个@ApiResponse()
@ApiResponse():注解接口方法,描述一个HTTP响应信息
code:HTTP返回值
message:返回信息
response:抛出异常的类