SpringBoot整合Swagger接口文檔工具的流程步驟
我們在開發(fā)接口的時候,會將接口文檔給前端的開發(fā)者進行對接。我們可以通過 Postman
或者 Yapi
等接口管理工具進行編寫管理。實際開發(fā)中,接口的管理確實也應該通過專業(yè)的工具管理。
那么,如果只是小團隊使用,我們是否可以在邊開發(fā)的過程中,順便把接口文檔給寫了呢?
當然,本文,我們就來談談怎么在 Spring Boot
整合 Swagger
接口文檔工具。
本文開發(fā)環(huán)境:
spring boot
版本2.1.3.RELEASE
java SDK
版本1.8
mac m1
系統(tǒng)
本文,在筆者之前的項目淺聊一下Spring Security的使用方法基礎上開發(fā)。
筆者嘗試了下整合 swagger3
,但是因為原先項目版本的問題,未能整合成功。故整合 swagger2
,文檔作用都一樣,就是頁面長得不一樣,可以放心使用。
添加依賴
我們在 pom.xml
中添加下面的依賴:
<!-- swagger --> <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> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency>
并在配置文件中添加配置:
spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
引入配置
在包 com.launch.config
中添加 SwaggerConfig.java
類:
package com.launch.config; 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.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.launch.controller")) // 接口所在的包 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Launch 系統(tǒng)") // 標題 .description("Jimmy Control System") // 描述 .version("1.0.0") // 版本 // 姓名,聯系 link,郵箱 .contact(new Contact("Jimmy", "https://juejin.cn/user/1996368846261294", "reng99@outlook.com")) .build(); } }
到此,我們運行項目,打開連接 http://localhost:8080/swagger-ui/index.html
,咦, 404
耶~
處理 404
版本的問題,使得我們無法讀取 swagger
包下面的頁面。那么,我們來重寫。
我們在 com.launch.config
中新增 WebMvcConfig.java
文件:
package com.launch.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); registry.addResourceHandler("doc.html") .addResourceLocations("classpath:/META-INF/resources/"); } }
重新啟動,訪問路徑 http://localhost:8080/doc.html
,就可以看到效果。
在本文淺聊一下Spring Security的使用方法 中,我們已經開發(fā)好了六個接口。點擊進入其中一個,比如 queryAll
查詢所有用戶的接口,可看到其文檔:
我們還可以對該接口進行調試:
感興趣的讀者可以自行嘗試。
以上就是SpringBoot整合Swagger接口文檔工具的流程步驟的詳細內容,更多關于SpringBoot整合Swagger的資料請關注腳本之家其它相關文章!
相關文章
SpringBoot使用@NotEmpty、@NotBlank、@NotNull注解進行參數校驗
我們經常需要對請求參數進行校驗,本文主要介紹了SpringBoot使用@NotEmpty、@NotBlank、@NotNull注解進行參數校驗,具有一定的參考價值,感興趣的可以了解一下2024-08-08Java JVM原理與調優(yōu)_動力節(jié)點Java學院整理
JVM是Java Virtual Machine(Java虛擬機)的縮寫,JVM是一種用于計算設備的規(guī)范,它是一個虛構出來的計算機,是通過在實際的計算機上仿真模擬各種計算機功能來實現的。下面通過本文給大家介紹jvm原理與調優(yōu)相關知識,感興趣的朋友一起學習吧2017-04-04Spring中的@Qualifier注解和@Resource注解區(qū)別解析
這篇文章主要介紹了Spring中的@Qualifier注解和@Resource注解區(qū)別解析,@Qualifier注解的用處是當一個接口有多個實現的時候,為了指名具體調用哪個類的實現,@Resource注解可以通過 byName命名和byType類型的方式注入,需要的朋友可以參考下2023-11-11