簡介:
Swagger 是一個規范和完整的框架,用于生成、描述、呼叫和可視化 RESTful 風格的 Web 服務,
也就是說只要我們配置好swagger,swagger就會自動幫我們生成api檔案,同時我們也可以對介面進行測驗,省去了后端開發人員對介面的撰寫,方便了介面的測驗,
添加依賴:
<!--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>
撰寫配置類
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
.paths(Predicates.not(PathSelectors.regex("/admin/.*")))
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("題目")
.description("介面檔案描述")
.version("1.0")
.contact(new Contact("zyw", "http://zyw.com", "1123@qq.com"))
.build();
}
}
為了增加api檔案的可讀性,可以在controller上加注解,從而給檔案加描述宣告:
@Api(description = "賬戶管理") //加在類上
@ApiOperation("查詢所有賬戶") //加在方法上
@ApiParam(value = "賬戶id",required = true) //加在屬性上
例如:


啟動spring boot專案 訪問:
http://127.0.0.1:8080/swagger-ui.html


可以直接在這里測驗,省去了api開發檔案的撰寫,也方便了介面測驗,
幫助到您請點贊關注收藏謝謝!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/216225.html
標籤:其他
上一篇:用字串實作加減法(C++實作)
