Swagger簡介
前後端分離
Vue + SpringBoot
後端時代:前端只用管理靜態頁面;html==> 後端。模板引擎 JSP =>後端是主力
前後端分離式時代:
- 後端 :後端控制層,服務層,數據訪問層 【後端團隊】
- 前端 :前端控制層,視圖層 【前端團隊】
- 僞造後端數據,json。已經存在了,不需要後端,前端工程依舊能夠跑起來
- 前端後如何交互 ? ===> API
- 前後端相對獨立,鬆耦合;
- 前後端甚至可以部署在不同的服務器上;
產生一個問題:
- 前後端集成聯調,前端人員和後端人員無法做到 “即使協商, 儘早解決”,最終導致問題集中爆發;
解決方案:
- 首先指定schema[計劃的提綱],實時更新最新API,降低集成的風險;
- 早些年:指定word計劃文檔;
- 前後端分離:
- 前端測試後端接口:postman
- 後端提供接口,需要實時更新最新的消息及改動!
Swagger特點
- 號稱世界上最流行的Api框架;
- RestFul Api 文檔在線自動生成工具=>Api文檔與API定義同步更新
- 直接運行,可以在線測試API接口;
- 支持多種語言:(Java,Php…)
官網:[https://swagger.io/
SpringBoot 集成Swagger的使用
在項目使用Swagger需要 springbox;
- swagger2
- ui
項目結構
-
新建一個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>
- 編寫一個Hello工程
helloController文件編寫:
package com.kuang.swagger.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.SQLOutput;
@RestController
public class helloController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
- 配置Swagger ==> Config
@Configuration //相當於component
@EnableSwagger2 //開啓Swagger2
public class SwaggerConfig {
}
- 測試運行:http://localhost:8080/swagger-ui.html
配置Swagger信息
package com.kuang.swagger.config;
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 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
public class SwaggerConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
// 配置swagger信息
private ApiInfo apiInfo() {
Contact contact = new Contact("小明", "https://blog.csdn.net/yalu_123456", "8718*****@qq.com");
return new ApiInfo("yalu 的api文檔",
"感謝現在努力的自己",
"v1.0",
"https://blog.csdn.net/yalu_123456",
contact,
"apache2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
測試結果:
Swagger配置掃描接口
swiggerconfig類:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
// @Bean
// public Docket docket(){
// return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
// }
//配置了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.kuang.swagger.controller"))
//paths()。過濾什麼路徑
.paths(PathSelectors.ant("/kuang/**"))
.build();
}
// 配置swagger信息
private ApiInfo apiInfo() {
Contact contact = new Contact("小明", "https://blog.csdn.net/yalu_123456", "8718*****@qq.com");
return new ApiInfo("yalu 的api文檔",
"感謝現在努力的自己",
"v1.0",
"https://blog.csdn.net/yalu_123456",
contact,
"apache2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
測試結果:
這裏我們可以看到經過.paths(PathSelectors.ant("/kuang/**"))過濾之後我們無法看到controller相關內容
配置是否啓動Swagger
同樣修改swaggerconfig類
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(false)//enable是否啓動Swagger,如果爲False,則Swagger不能再瀏覽器中訪問
.select()
.apis(RequestHandlerSelectors.basePackage("com.kuang.swagger.controller"))
//.paths(PathSelectors.ant("/kuang/**"))
.build();
}
// 配置swagger信息
private ApiInfo apiInfo() {
Contact contact = new Contact("小明", "https://blog.csdn.net/yalu_123456", "8718*****@qq.com");
return new ApiInfo("yalu 的api文檔",
"感謝現在努力的自己",
"v1.0",
"https://blog.csdn.net/yalu_123456",
contact,
"apache2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
.enable(false)//enable是否啓動Swagger,如果爲False,則Swagger不能再瀏覽器中訪問
測試結果
在特定環境下使用swagger
我只希望我的Swagger在生產環境中使用,在發佈的時候不使用?
- 判斷是不是生產環境 flag = false
- 注入enable(flag)
同樣我們需要修改swagger類:
//配置了Swagger的Docket的bean實例
@Bean
public Docket docket(Environment environment){
//設置要顯示的Swagger環境
Profiles profiles = Profiles.of("dev","test");
//通過environment.acceptsProfiles判斷是否處在自己設定的環境當中
boolean flag = environment.acceptsProfiles(profiles);
System.out.println(flag);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)//enable是否啓動Swagger,如果爲False,則Swagger不能再瀏覽器中訪問
.select()
.apis(RequestHandlerSelectors.basePackage("com.kuang.swagger.controller"))
//.paths(PathSelectors.ant("/kuang/**"))
.build();
}
// 配置swagger信息
private ApiInfo apiInfo() {
Contact contact = new Contact("小明", "https://blog.csdn.net/yalu_123456", "8718*****@qq.com");
return new ApiInfo("yalu 的api文檔",
"感謝現在努力的自己",
"v1.0",
"https://blog.csdn.net/yalu_123456",
contact,
"apache2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
這裏還需要設置增加兩個配置文件
application.properties
spring.profiles.active=pdiv
applicaton-devproperties
server.port=8081
application-pro.peoperties
server.port=8082
這裏我們需要注意
//設置要顯示的Swagger環境
Profiles profiles = Profiles.of(“dev”,“test”);
這裏我們設置了兩個字段“dev”和“test”,這裏只要是application.properties文件中激活的字段都可以使用swagger,不過需要特別注意訪問的端口號!!!(容易入坑)
我們現在來測試下:
注意訪問端口:8081
配置API文檔的分組
只要我們的接口中,返回值中存在實體類,他就會被掃描到Swagger中
實體類:
package com.kuang.swagger.pojo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
//@Api(註釋)
@ApiModel("用戶實體類")
public class User {
@ApiModelProperty("用戶名")
public String username;
@ApiModelProperty("密碼")
public String password;
}
controller:
package com.kuang.swagger.controller;
import com.kuang.swagger.pojo.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.SQLOutput;
@RestController
public class helloController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
//只要我們的接口中,返回值中存在實體類,他就會被掃描到Swagger中
@PostMapping(value = "/user")
public User user(){
return new User();
}
//Operation接口,不是放在類上的,是方法
@ApiOperation("Hello控制類")
@GetMapping(value = "/hello2")
public String hello2(@ApiParam("用戶名") String username){
return "hello"+username;
}
@ApiOperation("Post測試類")
@PostMapping(value = "/postt")
public User postt(@ApiParam("用戶名") User user){
int i = 5;
return user;
}
}
測試結果:
總結
- 我們可以通過Swagger給一些比較難理解的屬性或者接口,增加註釋信息
- 接口文檔實時更新
- 可以在線測試
Swagger是一個優秀的工具,幾乎所有大公司都有使用它
【注意點】在正式發佈的時候,關閉Swagger!!!出於安全考慮。而且節省運行的內存;