Swagger
前言
使用Swagger的好处
1.RestFul Api 文档在线自动生成工具=>Api文档与API定义同步更新。
2.可以直接运行,在线测试API接口。
3.支持多种语言。
官网
http://swagger.io/
使用的具体东西
在项目中使用Swagger需要springbox;
swagger2
ui
配置Swagger
1.导入Swagger的Jar包(配置Maven)
<dependency>
<!--添加Swagger依赖 -->
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<!--添加Swagger-UI依赖 -->
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
2.配置Swagger的Config
在config组里创建SwaggerCofig类,
通过注解
@Configuration
@EnableSwagger2
开启,注意第一个注解只是单纯的@Component
的意思,第二个@EnableSwagger2
是开启了Swagger2。
package com.jic.myspringboot.Config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
3.测试
开启后访问localhost:8080/swagger-ui.html
4.配置Swagger类的bean实例Docket及apiInfo
package com.jic.myspringboot.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import java.util.ArrayList;
import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//配置了Swagger的bean实例;
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
private ApiInfo apiInfo(){
Contact contact=new Contact("李贺雲","https://me.csdn.net/Gakooon","1034108904@qq.com");
return new ApiInfo("公司员工体检系统"
, "practice demo"
, "1.0"
, "https://me.csdn.net/Gakooon"
, contact
, "Apache 2.0"
, "http://www.apache.org/licenses/LICENSE-2.0"
, new ArrayList());
}
}
4.配置扫描接口
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//配置了Swagger的bean实例;
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//在这后面可以设置是否启动,如果不启动就把.enabled(false)
.select()
// RequestHandlerSelectors 配置要扫描接口的方式。
//.basePackage("com.jic.myspringboot.controller") 扫描指定的包 主流方法
//.any() 扫描全部
//.none() 不扫描
//.withClassAnnotation(RestController.class) 扫描类上的注解
//.withMethodAnnotation(GetMapping.class)
.apis(RequestHandlerSelectors.basePackage("com.jic.myspringboot"))
//.paths(PathSelectors.ant("/Person/**")) 过滤作用,设置条件
.paths(PathSelectors.ant("/Person/**"))
.build();
}
private ApiInfo apiInfo(){
Contact contact=new Contact("李贺雲","https://me.csdn.net/Gakooon","1034108904@qq.com");
return new ApiInfo("公司员工体检系统"
, "practice demo"
, "1.0"
, "https://me.csdn.net/Gakooon"
, contact
, "Apache 2.0"
, "http://www.apache.org/licenses/LICENSE-2.0"
, new ArrayList());
}
}
6.配置项目环境
7.常用的注解
@ApiModel("")放在最前头,给类加上注释。
@ApiModelProperty("")给实体类加上注释方便人家看懂
@ApiOperation("")在方法上放一个注释,来解释方法
@ApiParm("")在传参前面,注释参数