Swagger简介
前后端分离 Vue + SpringBoot
后端时代:前端只用管理静态页面;html => 后端。模板引擎 JSP => 后端是主力
前后端分离时代:
- 后端:后端控制层、服务层、数据访问层 【后端团队】
- 前端:前端控制层,视图层 【前端团队】
- 伪造后端数据,Json。已经存在了,不需要后端,前端功能依旧能够跑起来
- 前后端如何交互? ===》API接口
- 前后端相对独立,松耦合;
- 前后端甚至可以部署在不同的服务器上;
产生一个问题:
- 前后端集成联调,前端人员和后端人员无法做到,及时协调,尽早解决;最终导致问题集中爆发。
解决方案:
- 首先指定一个Schema[计划的提纲],实时更新最新API,降低集成的风险
- 早些年:指定 word 计划文档;
- 前后端分离:
- 前端测试后端接口:postman
- 后端提供接口,需要实时更新最新的小写及改动!
实操:
1、创建一个SpringBoot项目
2、引入maven依赖
<!-- swagger依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
3、增加一个配置类(配置文档信息、和基础配、设置什么环境进行开启Swagger)
注意:SpringBoot的yml文件需要配置项目的profile
package com.swagger.config;
import com.swagger.controller.HelloController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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;
@Configuration // 代表配置类
@EnableSwagger2 // 开启Swagger2
public class SwaggerConfig {
// 配置了 Swagger 的 Docket 的 Bean 实例
@Bean
public Docket docket(Environment environment){
// 设置要显示的 Swagger 环境
Profiles profiles = Profiles.of("dev","test");
// 获取项目的环境,将之传到 .enable() 设置中
// 通过environment.acceptsProfiles 判断当前环境是否处在自己设定的环境中,是返回true
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("A") //分组的组名
.enable(flag) // 是否开启Swagger false 则不能访问,
.select()
// RequestHandlerSelectors 配置要扫描接口的方式;
// RequestHandlerSelectors.any() 扫描全部
.apis(RequestHandlerSelectors.basePackage("com.swagger.controller")) // 扫描controller
// .paths() 过滤什么路径
.build(); // build 工厂模式,创建什么东西
}
// 配置 Swagger 信息 apiInfo
private ApiInfo apiInfo(){
// 作者信息
Contact DEFAULT_CONTACT = new Contact("Allen", "暂时没有", "暂时没有");
return new ApiInfo(
"xxx 的 Swagger API 文档",
"这个作者有点酷",
"v1.0",
"urn:tos",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
4、接口配置Swagger注解:
package com.swagger.controller;
import com.swagger.pojo.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
@Api("控制层")
@RestController
public class HelloController {
@GetMapping(value = "/hello")
public String hello(){
return "hello";
}
// 只要我们的接口中,返回值中存在实体类,它就会被 Swagger 扫描
@PostMapping(value = "/user")
public User user(){
return new User();
}
@ApiOperation("hello1接口") // 接口加上注释(不是放在类上的,是放在方法上的)
@GetMapping("/hello1")
@ApiImplicitParams({
@ApiImplicitParam(name = "username",value = "用户名",required = true,dataType = "String",paramType = "")
})
public String hello1(@ApiParam(name = "username",value = "用户名") String username){
return "hello"+username;
}
}