SpringBoot整合Swagger2的完整過程記錄
前言
springBoot作為微服務首選框架,為其他服務提供大量的接口服務。接口對接方需要實時最近的接口文檔。
swagger可以通過代碼和注釋自動為web項目生成在線文檔,這里使用swagger。
當 SpringBoot 代碼中 SpringMVC 使用自動化配置類 WebMvcAutoConfiguration 時,其整合 Swagger2 的方法如下。
如果 SpringMVC 的配置過程使用了 WebMvcConfigurationSupport;則如下的整合方法不適合。
一、Spring Boot Web 整合 Swagger2 過程
Spring Boot Web 項目整合 Swagger2 主要有兩個過程:
- 添加 Swagger2 相關(guān)依賴。
- 配置 Swagger2 配置類。
1.1、添加 Swagger2 相關(guān)依賴
首先要對 Spring Boot Web 的項目,添加 Swagger2 相關(guān)的依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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>
1.2、配置 Swagger2 配置類
@Configuration @EnableSwagger2 public class Swagger { //創(chuàng)建 Docket 的Bean @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) //select() 函數(shù)返回一個 ApiSelectorBuilder實例用來控制哪些接口暴露給 Swagger 來展現(xiàn) .select() //要掃描的包 .apis(RequestHandlerSelectors.basePackage("com.example.controller")) //選擇API路徑 .paths(PathSelectors.any()) .build(); } //創(chuàng)建文檔的基本信息 public ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("Swagger UI 的標題") .description("用restful風格寫接口") .termsOfServiceUrl("") .version("1.0") .build(); } }
二、配置 Swagger2 接口常用注解
2.1、@Api 請求類說明
寫在controller類定義上方,用于說明類的作用。
@Api(value = "Swagger Test Control", description = "演示Swagger用法的Control類", tags = "Swagger Test Control Tag")
2.2、@ApiOperation 方法的說明
寫在REST接口上方,用于說明方法的作用。
@ApiOperation( value="創(chuàng)建用戶", notes="根據(jù)User對象創(chuàng)建用戶")
2.3、@ApiImplicitParams 和 @ApiImplicitParam 方法參數(shù)說明
@ApiImplicitParams:用在請求的方法上,包含一組參數(shù)說明 @ApiImplicitParam:對單個參數(shù)的說明 name:參數(shù)名 value:參數(shù)的漢字說明、解釋 required:參數(shù)是否必須傳 paramType:參數(shù)放在哪個地方 · header --> 請求參數(shù)的獲?。篅RequestHeader · query --> 請求參數(shù)的獲取:@RequestParam · path(用于restful接口)--> 請求參數(shù)的獲?。篅PathVariable · body(請求體)--> @RequestBody User user · form(普通表單提交) dataType:參數(shù)類型,默認String,其它值dataType="int" defaultValue:參數(shù)的默認值 -------------------------------------------------------------------- @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "ID", dataType = "Long"), @ApiImplicitParam(name = "user", value = "用戶", dataType = "User") })
2.4、@ApiResponses 和 @ApiResponse 方法返回值的說明
@ApiResponses:方法返回對象的說明 @ApiResponse:每個參數(shù)的說明 code:數(shù)字,例如400 message:信息,例如"請求參數(shù)沒填好" response:拋出異常的類 ------------------------------------------------------------------- @ApiResponses({ @ApiResponse(code = 400, message = "權(quán)限不足"), @ApiResponse(code = 500, message = "服務器內(nèi)部異常") } )
2.5、@ApiModel 和 @ApiModelProperty
@ApiModel 用于JavaBean 上面,表示一個JavaBean。這種一般用在post創(chuàng)建的時候,使用 @RequestBody 這樣的場景,請求參數(shù)無法使用 @ApiImplicitParam 注解進行描述的時候。
@ApiModelProperty 用對象接收參數(shù)時,描述對象的一個字段。
@ApiModel( description = "學生") public class Student { @ApiModelProperty(value = "主鍵id") private String id; @ApiModelProperty(value = "名稱", required = true) private String name; @ApiModelProperty(value = "年齡", required = true) private int age; }
2.6、其他注解
@ApiIgnore :使用該注解忽略這個API,不對這個接口生成文檔。
@ApiError:發(fā)生錯誤返回的信息
總結(jié)
到此這篇關(guān)于SpringBoot整合Swagger2的文章就介紹到這了,更多相關(guān)SpringBoot整合Swagger2內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java代碼統(tǒng)計網(wǎng)站中不同省份用戶的訪問數(shù)
這篇文章主要介紹了Java代碼統(tǒng)計網(wǎng)站中不同省份用戶的訪問數(shù) 的相關(guān)資料,非常具有參考借鑒價值,感興趣的朋友一起學習吧2016-05-05SpringBoot使用Validation進行參數(shù)校驗的示例詳解
在 SpringBoot項目開發(fā)中,有一個觀點是不要相信前端傳入的參數(shù),因為你不知道用戶是怎么操作我們接口的,所以在后端也需要對參數(shù)進行校驗,這篇文章主要講講我們項目中最常使用的驗證方案2023-05-05Springboot實現(xiàn)動態(tài)定時任務管理的示例代碼
最近在做spring boot項目開發(fā)中,由于使用@EnableScheduling注解和@Scheduled注解來實現(xiàn)的定時任務,只能靜態(tài)的創(chuàng)建定時任務,不能動態(tài)修改、添加、刪除、啟/停任務,下面通過本文給大家介紹Springboot實現(xiàn)動態(tài)定時任務管理的方法,感興趣的朋友跟隨小編一起看看吧2023-07-07關(guān)于Selenium的UI自動化測試屏幕截圖功能實例代碼
今天小編就為大家分享一篇關(guān)于Selenium的UI自動化測試屏幕截圖功能實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05