SpringBoot集成Swagger2生成接口文檔的方法示例
我們提供Restful接口的時候,API文檔是尤為的重要,它承載著對接口的定義,描述等。它還是和API消費方溝通的重要工具。在實際情況中由于接口和文檔存放的位置不同,我們很難及時的去維護文檔。個人在實際的工作中就遇到過很多接口更新了很久,但是文檔卻還是老版本的情況,其實在這個時候這份文檔就已經(jīng)失去了它存在的意義。而 Swagger
是目前我見過的最好的API文檔生成工具,使用起來也很方便,還可以直接調(diào)試我們的API。我們今天就來看下 Swagger2
與 SpringBoot
的結(jié)合。
準備工作
一個SpringBoot項目,可以直接去官網(wǎng) 生成一個demo 。
一個用戶類。
package cn.itweknow.springbootswagger.model; import java.io.Serializable; /** * @author ganchaoyang * @date 2018/12/19 10:29 * @description */ public class User implements Serializable { private Integer id; private String name; private String password; private String email; }
項目依賴
Web Service肯定是一個Web項目,所以我們這里依賴了 spring-boot-starter-web
包,其他兩個包就是和 Swagger
相關(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>
Swagger配置
Springfox Docket實例為Swagger配置提供了便捷的配置方法以及合理的默認配置。我們將通過創(chuàng)建一個Docket實例來對Swagger進行配置,具體配置如下所示。
@Configuration @EnableSwagger2 public class SwaggerConfig extends WebMvcConfigurationSupport { @Bean public Docket productApi() { return new Docket(DocumentationType.SWAGGER_2).select() // 掃描的包路徑 .apis(RequestHandlerSelectors.basePackage("cn.itweknow.springbootswagger.controller")) // 定義要生成文檔的Api的url路徑規(guī)則 .paths(PathSelectors.any()) .build() // 設(shè)置swagger-ui.html頁面上的一些元素信息。 .apiInfo(metaData()); } private ApiInfo metaData() { return new ApiInfoBuilder() // 標題 .title("SpringBoot集成Swagger2") // 描述 .description("這是一篇博客演示") // 文檔版本 .version("1.0.0") .license("Apache License Version 2.0") .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0") .build(); } @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
上述代碼中的addResourceHandlers方法添加了兩個資源處理程序,這段代碼的主要作用是對Swagger UI的支持。
API文檔
好了,到這一步,我們已經(jīng)在一個SpringBoot項目中配置好了Swagger?,F(xiàn)在,我們就來看一下如何去使用他。首先我們定義了一個 Controller
并提供了兩個接口:
- 通過用戶id獲取用戶
- 用戶登錄
@RestController @RequestMapping("/user") @Api(description = "用戶相關(guān)接口") public class UserController { /** * 通過id查詢用戶 * @return */ @RequestMapping(value = "/get", method = RequestMethod.GET) @ApiOperation("根據(jù)id獲取用戶") public User getUserById(@ApiParam(value = "用戶id") Integer id) { return new User(); } @RequestMapping(value = "/login", method = RequestMethod.POST) @ApiOperation("用戶登錄") public User login(@RequestBody User user) { return new User(); } }
相信大家都注意到了,這個 Controller
里面多了很多新的注解,比如說 @Api
, @ApiOperation
等,下面我們就來一一解釋一下。
- @Api,這個注解是用在Controller類上面的,可以對Controller做必要的說明。
- @ApiOperation,作用在具體的方法上,其實就是對一個具體的API的描述。
- @ApiParam,對API參數(shù)的描述。
到這里,其實我們的Swagger就已經(jīng)可以有效果了,讓我們將項目運行起來先看看效果。訪問 http://localhost:8080/swagger-ui.html 即可。
Model
在上面的圖中可以看到在頁面的下方有一個Models的標簽,那么這個是啥呢。其實這個就是我們API中出現(xiàn)的一些對象的文檔,我們也可以通過注解來對這些對象中的字段做一些說明,以方便使用者理解。以文章開頭提到的 User
類來做一個說明。
@ApiModel("用戶實體") public class User implements Serializable { @ApiModelProperty(value = "用戶id") private Integer id; @ApiModelProperty(value = "用戶名稱", required = true) private String name; @ApiModelProperty(value = "密碼", required = true) private String password; @ApiModelProperty(value = "用戶郵箱") private String email; }
我們來看一下 User
類在Swagger上是如何展示的:
有一個細節(jié),那就是required = true的字段上面被紅星修飾,代表了必填項。
API測試
在 swagger-ui.html
頁面上我們可以直接測試API,如下圖所示,點擊 Try it out
,然后填寫參數(shù),并點擊 Execute
即可進行調(diào)用。
好了,對于Swagger的介紹就到這里了,最后奉上本文的源碼地址, 請戳這里。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決springboot接入springfox-swagger2遇到的一些問題
這篇文章主要介紹了解決springboot接入springfox-swagger2遇到的一些問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07mybatis中數(shù)據(jù)加密與解密的實現(xiàn)
數(shù)據(jù)加解密的實現(xiàn)方式多種多樣,在mybatis環(huán)境中數(shù)據(jù)加解密變得非常簡單易用,本文主要介紹了mybatis中數(shù)據(jù)加密與解密的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03java 數(shù)據(jù)結(jié)構(gòu)之堆排序(HeapSort)詳解及實例
這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)之堆排序(HeapSort)詳解及實例的相關(guān)資料,需要的朋友可以參考下2017-03-03SpringBoot+Shiro學(xué)習(xí)之密碼加密和登錄失敗次數(shù)限制示例
本篇文章主要介紹了SpringBoot+Shiro學(xué)習(xí)之密碼加密和登錄失敗次數(shù)限制示例,可以限制登陸次數(shù),有興趣的同學(xué)可以了解一下。2017-03-03