Swagger简介
前后端分离
Vue+SpringBoot
前端:后端控制层,服务层,数据访问层
前端:前端控制层,视图层
前后端如何交互?==>API
前后端相对独立,并且松耦合;
前后端甚至可以部署在不同的服务器上;
产生一个问题:
前后端集成联调,前端人员和后端人员,无法做到及时协商。
解决方案:
首先指定schema【计划的提纲】,实时更新最新的API,降低集成的风险;
Swagger
- 号称世界上最流行的Api框架;
- RestFul Api 文档在线自动生成工具=> Api文档与Api定义同步更新
- 直接运行,可以在线测试API接口
- 支持多种语言
在项目中使用swagger需要springbox;
- swagger2
- ui
SpringBoot集成Swagger
1,新建项目
2,导入依赖
< dependency>
< groupId>io.springfox< /groupId>
< artifactId>springfox-swagger2< /artifactId>
< version>3.0.0< /version>
< /dependency>
< dependency>
< groupId>io.springfox< /groupId>
< artifactId>springfox-swagger-ui< /artifactId>
< version>3.0.0< /version>
< /dependency>
3,新建hello工程
4,配置Swagger==>@Configuration
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
}
5,测试运行
进入http://localhost:8080/swagger-ui.html
配置Swagger
Swagger的bean实例Docket;
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
//配置了Swagger的Docket的Bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
//配置Swagger信息apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact(“华”,“http://baidu.com”,“791812@q.com”);
return new ApiInfo(
“华的SwaggerApi文档”,
“明德任责 致知力行”, “1.0”,
“http://baidu.com”, contact,
“Apache 2.0”,
“http://www.apache.org/licenses/LICENSE-2.0”,
new ArrayList());
}
}
Swagger配置扫描接口
public class SwaggerConfig {
//配置了Swagger的Docket的Bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//RequestHandlerSelectors配置要扫描接口的方式
//basePackage指定要扫描的包
//any扫描全部
//none不扫描
//withClassAnnotation扫描类上的注解,参数是一个注解的反射对象
//withMethodAnnotation扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage(“com.hua.controller”))
//paths过滤 什么路径
.paths(PathSelectors.ant("/华/**"))
.build();
}
配置是否启动Swagger
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(false) //是否启用Swagger,如果是false则Swagger不能在浏览器中访问
我只希望我的Swagger在生产环境中使用,在发布的时候不使用?
- 判断是不是生产环境 flag = false
- 注入enable(flag)
-
public class SwaggerConfig {
//配置了Swagger的Docket的Bean实例
@Bean
public Docket docket(Environment environment){
//设置要现实的Swagger环境
Profiles profiles = Profiles.of(“dev”,“test”);
//获取项目的环境
//通过environment.acceptsProfiles判断是否处在自己设定的环境当中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)//是否启用Swagger,如果是false则Swagger不能在浏览器中访问
配置API文档的分组
.groupName(“华”)
如何配置多个分组多个Docket实例即可
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("A");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("B");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2).groupName("C");
}
Swagger注解
实体类配置
@ApiModel(“用户实体类”)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@ApiModelProperty(“用户名”)
public String username;
@ApiModelProperty(“密码”)
public String password;
}
控制器接口配置
@RestController
@RequestMapping("/")
@Api(“hello控制器”)
public class HelloController {
@ApiOperation(value = “hello接口”,httpMethod = “Get”)//接口 给接口加了一个中文注释
@GetMapping("/hello")
public String hello(){
return “Hello”;
}
//只要我们的接口中,返回值中存在实体类,它就会被扫描到Swagger中
@PostMapping("/user")
public User user(){
return new User();
}
@GetMapping("/hello2")
public String hello2(@ApiParam(“用户名”) String username){
return username;
}
}
总结
1,我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息
2,接口文档实时更新
3,可以在线测试
Swagger是一个优秀的工具,几乎所有大公司都有使用它
**
注意点
在正式发布时,关系Swagger!!!!!!!!!出于安全考虑,而且节省内存。**