Swagger使用
一.pom文件中添加Swagger依赖
<!--swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <!--swagger-->
二.创建Swaggerconfig类:Swagger2Config
package com.github.xxx.security.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RestController; 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.Parameter; 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.List; @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi() { List<Parameter> pars = new ArrayList<Parameter>(); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) .paths(PathSelectors.any()) .build() .globalOperationParameters(pars) .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Lark API") .description("测试接口") .termsOfServiceUrl("https://xxxxxx") .version("1.0") .build(); } }
三.运行项目
四.访问前端界面查看接口
http://项目地址:微服务端口/swagger-ui.html#/
五.Swagger注解
1、@Api():用在请求的类上,表示对类的说明,也代表了这个类是swagger2的资源
1)tags:说明该类的作用,参数是个数组,可以填多个。例:
@Api(tags = "控制类集合接口") 效果如下:
2)description: "接口描述"。例:
@Api(tags = "控制类集合接口",description = "这是一些描述控制类的接口") 效果如下
2.@ApiOperation():用于方法,表示一个http请求访问该方法的操作
1)value="接口名称"