這篇文章主要講解了“Swagger接口說明文檔的配置和使用方法”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Swagger接口說明文檔的配置和使用方法”吧!
Swagger是一個開源的接口配置文檔,一般用于前后端分離,代替后端人員為前端人員書寫繁瑣的接口文檔,使后端人員從繁瑣的接口文檔中解脫出來。Swagger如何使用呢?首先我們要在springboot中的pom文件引入依賴包
<!-- swagger依賴組件 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
其次,添加swagger的配置文件,該配置文件定義了網頁訪問swagger2的路徑以及標題,描述等信息
package com.xash.quartzDemo.config;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configurable
public class SwaggerConfig{
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("測試模塊")
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xash.quartzDemo.controller"))
.paths(PathSelectors.regex(".*/.*"))
.build();
}
private ApiInfo getApiInfo(){
return new ApiInfoBuilder()
.title("測試模塊")
.description("小標題")
.version("1.0")
.build();
}
@Bean
public Docket api1(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("測試模塊1")
.apiInfo(getApiInfo1())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xash.quartzDemo.controller"))
.paths(PathSelectors.regex(".*/.*"))
.build();
}
@Bean
private ApiInfo getApiInfo1(){
return new ApiInfoBuilder()
.title("測試模塊1")
.description("小標題")
.version("1.0")
.build();
}
}這樣,我們就可以在要添加接口文檔的地方利用注解添加相應的接口文檔信息了
例如:
@Api(tags="這是個測試Controller")
public class PermissionController {}類上加注解,說明該類具有的功能
@ApiOperation(value = "根據用戶id查詢權限")
public String selectPermissionById(ModelMap map,@ApiParam("id") @RequestParam("id")int id){
System.out.println("開始查詢");
map.put("name", "歡飲使用thymeleaf模板引擎");
map.put("sysPermission", permissionService.selectPermissionById(id));
return "index";
}方法上加注解,表示方法的功能,以及可見在形參上加注解,指定形參,對形參進行描述
訪問地址默認為192.168.2.199:8080/項目應用/swagger2-ui.html
感謝各位的閱讀,以上就是“Swagger接口說明文檔的配置和使用方法”的內容了,經過本文的學習后,相信大家對Swagger接口說明文檔的配置和使用方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。