javascript
Springboot2 Swagger3 集成
文章目錄
- 一、默認UI
- 1. 版本嘗鮮
- 2. 導入依賴
- 3. Swagger3Config配置類
- 4. Swagger3.0常用注解
- 4.Controller 層使用Swagger3注解例子
- 5.訪問Swagger3接口文檔界面
- 6.Swagger3接口文檔界面展示
- 二、bootstrapUI
- 2.1. 導入依賴
- 2.2. 訪問地址
一、默認UI
1. 版本嘗鮮
Swagger3在Swagger2的基礎上進行了部分升級, 使用和Swagger2沒有多少區別。
一個重要的優化是依賴的引入,由之前的多個依賴變更為一個依賴,跟隨springboot-starter風格,同時引入了新的開關注解 @EnableOpenApi 以代替@EnableSwagger2 。
因此,集成工作變得更加的簡便了,必要工作只有兩個,添加swagger3的starter依賴包,在springboot主程序類添加@EnableOpenApi開關注解。
2. 導入依賴
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency></dependencies>3. Swagger3Config配置類
(必選)添加開關注解@EnableOpenApi
package com.gblfy.common.config;import io.swagger.annotations.ApiOperation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.oas.annotations.EnableOpenApi; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket;/*** @author gblfy*/ //@EnableSwagger2 是 swagger2.0版本的注解 //swagger在3.0之后換成了@EnableOpenApi @Configuration @EnableOpenApi public class SwaggerConfig {@Beanpublic Docket createRestApi(){return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any()).build();}private ApiInfo apiInfo(){return new ApiInfoBuilder().title("Swagger3接口文檔").description("適用于前后端分離統一的接口文檔").version("1.0").build();} }4. Swagger3.0常用注解
@Api:用在請求的類上,表示對類的說明
??tags=“說明該類的作用,可以在UI界面上看到的注解”
??value=“該參數沒什么意義,在UI界面上也看到,所以不需要配置”
@ApiOperation:用在請求的方法上,說明方法的用途、作用
??value=“說明方法的用途、作用”
??notes=“方法的備注說明”
@ApiImplicitParams:用在請求的方法上,表示一組參數說明
??@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數的各個方面
????name:參數名
????value:參數的漢字說明、解釋
????required:參數是否必須傳
??? paramType:參數放在哪個地方
????· header --> 請求參數的獲取:@RequestHeader
????· query --> 請求參數的獲取:@RequestParam
????· path(用于restful接口)–> 請求參數的獲取:@PathVariable
???? · div(不常用)
????· form(不常用)
????dataType:參數類型,默認String,其它值dataType=“Integer”
????defaultValue:參數的默認值
@ApiResponses:用在請求的方法上,表示一組響應
??@ApiResponse:用在@ApiResponses中,一般用于表達一個錯誤的響應信息
????code:數字,例如400
????message:信息,例如"請求參數沒填好"
????response:拋出異常的類
@ApiModel:用于響應類上,表示一個返回響應數據的信息(這種一般用在post創建的時候,使用@RequestBody這樣的場景,
請求參數無法使用@ApiImplicitParam注解進行描述的時候)
@ApiModelProperty:用在屬性上,描述響應類的屬性
4.Controller 層使用Swagger3注解例子
package com.gblfy.controller;import com.infoshare.service.IUserService; import com.infoshare.util.SendMailUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.scheduling.annotation.Async; import javax.annotation.Resource; import javax.servlet.http.HttpSession;/*** @author: gblfy* @time: 2021/9/17*/ @Api(tags = "用戶信息處理") @RestController @RequestMapping("/user") public class UserController {@Resource(name = "userServiceImpl")private IUserService userService;@Resource(name = "sendMailUtil")private SendMailUtil sendMailUtil;private final static int AUTH_CODE_VALID_TIME = 600; //驗證碼失效時間為 10 min/*** 異步獲得驗證碼的接口* 驗證碼存儲到 Session 里面* @param mail 郵箱* @return authCode_*/@ApiOperation("用戶獲得注冊驗證碼")@Async@GetMapping("/getAuthCode")public String getAuthCode(@RequestParam(name = "mail") String mail,HttpSession session){String authCode_ = sendMailUtil.sendMailAndGetAuthCode(mail);session.setAttribute("mail",authCode_);session.setMaxInactiveInterval(AUTH_CODE_VALID_TIME); //設置驗證碼失效時間為10minreturn authCode_;}}5.訪問Swagger3接口文檔界面
Swagger的訪問路徑由port/swagger-ui.html改成了 port/swagger-ui/ 或port/swagger-ui/index.html
兩種訪問方式任選其一
http://localhost:8080/swagger-ui/
http://localhost:8080/swagger-ui/index.html
6.Swagger3接口文檔界面展示
二、bootstrapUI
2.1. 導入依賴
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>swagger-bootstrap-ui</artifactId><version>1.9.6</version></dependency>2.2. 訪問地址
http://localhost:8080/doc.html
總結
以上是生活随笔為你收集整理的Springboot2 Swagger3 集成的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot2 中 spring
- 下一篇: SpringBoot 使用 Caffei