Swagger超详细学习笔记
学习目标:
- 了解swagger的作用和概念
- 了解前后端分离
- 在SpringBoot中集成swagger
Swagger简介
前后端分离
Vue + SpringBoot
后端时代:
前端只用管理静态页面;html ==> 后端。模板引擎 JSP ==> 后端是主力
前后端分离时代:
- 后端:后端控制层,服务层,数据访问层(controller,service,dao)【后端团队】
- 前端:前端控制层,视图层【前端团队】
- 伪造后端数据,json。已经存在了,不需要后端,前端工程依旧能够运行。
- 前后端如何交互? == > API
- 前后端相对独立,松耦合;
- 前后端可以部署在不同的服务器上;
产生一个问题:
- 前后端集成联调,前端人员和后端人员无法做到“即时协商,尽早解决”。最后导致问题集中爆发。
解决方案:
- 首先制定一个schema【计划的提纲】,实时更新最新的API,降低集成的风险;
- 早些年:制定word文档;
- 前后端分离:
- 前端测试后端接口:postman
- 后端提供接口,需要实时更新最新的消息及改动;
Swagger
- 号称世界上最流行的API框架;
- RestFul API文档在线自动生成工具 == > API文档与API定义同步更新;
- 直接运行,可以在线测试API接口;
- 支持多种语言:Java,Php。。。。
官网:
https://swagger.io/
在项目中使用Swagger
需要springbox;
- swagger2
- ui
SpringBoot集成Swagger
步骤:
-
新建一个SpringBoot-web项目
-
导入相关依赖包
<!-- 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.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.2</version>
</dependency>
-
编写一个helloword项目
-
配置Swagger ==> config
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
}
可以访问ui界面了(推荐使用后者bootstrap的界面)
http://localhost:8080/swagger-ui.html
http://localhost:8081/doc.html
Swagger浏览器访问报错 Unable to infer base url.
解决方法:主启动类加上@ComponentScan(“swagger配置类所在包”)以保证配置类被扫描到。
@SpringBootApplication()
@ComponentScan("com.demo.ceshi.config")//根据自己需要填写包名
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
}
swagger报错 java.lang.NumberFormatException: For input string: “”
配置文件加上
logging.level.io.swagger.models.parameters.AbstractSerializableParameter=error
配置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("加林", "https://space.bilibili.com/102777715", "2079845319@qq.com");
return new ApiInfo("加林的Swagger文档",
"课工场KH83", "1.0",
"https://space.bilibili.com/102777715",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
Swagger配置扫描接口
Docket.select()
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//enable,是否启用Swagger,默认为true,如果为false,Swagger就不能在浏览器访问
//.enable(false)
.select()
//RequestHandlerSelectors,配置要扫描接口的方式
//basePackage,指定要扫描的包,一般是用这个
//any(),扫描全部
//none(),都不扫描
//withClassAnnotation,扫描类上的注解,参数是一个注解的反射对象
//withMethodAnnotation,扫描方法上的注解
//.apis(RequestHandlerSelectors.basePackage("com.hjl.controller"))
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
//paths(),过滤什么路径
.paths(PathSelectors.ant("/hjl/**"))
.build();
}
配置是否启动Swagger
//enable,是否启用Swagger,默认为true,如果为false,Swagger就不能在浏览器访问
//.enable(false)
问题:我只希望我的Swagger在生产环境中使用,在发布的时候不使用?
- 判断是不是生产环境 flag = false
- 注入enable(flag)
//设置要显示的Swagger环境
Profiles profiles = Profiles.of("dev","test");
//获取项目的环境(dev,prod,sit...)
//通过environment.acceptsProfiles判断是否处在自己设定的环境当中
boolean flag = environment.acceptsProfiles(profiles);
//enable,是否启用Swagger,默认为true,如果为false,Swagger就不能在浏览器访问
//.enable(flag)
怎么配置API文档的分组
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("星空")
如何配置多个分组?
多个Docket实例即可。
@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");
}
实体类配置
@ApiModel("用户实体类") //给生成的文档加上注释
public class User {
@ApiModelProperty("用户名")
public String userName;
@ApiModelProperty("用户密码")
public String userPwd;
public User(String userName, String userPwd) {
this.userName = userName;
this.userPwd = userPwd;
}
}
//只要我们的接口中,返回值中存在实体类,它就会被扫描到Swagger中
@PostMapping(value = "/user")
public User user(){
return new User("加林","123456");
}
//Operation接口,不是放在类上,是方法上,加注释
//@ApiParam("用户名"),给参数加注释
@ApiOperation("hello控制")
@GetMapping(value = "/hello2")
public String hello2(@ApiParam("用户名") String userName){
return "hello2"+userName;
}