SpringBoot整合Swagger接口文檔工具的流程步驟
我們?cè)陂_發(fā)接口的時(shí)候,會(huì)將接口文檔給前端的開發(fā)者進(jìn)行對(duì)接。我們可以通過 Postman
或者 Yapi
等接口管理工具進(jìn)行編寫管理。實(shí)際開發(fā)中,接口的管理確實(shí)也應(yīng)該通過專業(yè)的工具管理。
那么,如果只是小團(tuán)隊(duì)使用,我們是否可以在邊開發(fā)的過程中,順便把接口文檔給寫了呢?
當(dāng)然,本文,我們就來談?wù)勗趺丛?Spring Boot
整合 Swagger
接口文檔工具。
本文開發(fā)環(huán)境:
spring boot
版本2.1.3.RELEASE
java SDK
版本1.8
mac m1
系統(tǒng)
本文,在筆者之前的項(xiàng)目淺聊一下Spring Security的使用方法基礎(chǔ)上開發(fā)。
筆者嘗試了下整合 swagger3
,但是因?yàn)樵软?xiàng)目版本的問題,未能整合成功。故整合 swagger2
,文檔作用都一樣,就是頁面長得不一樣,可以放心使用。
添加依賴
我們?cè)?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)") // 標(biāo)題 .description("Jimmy Control System") // 描述 .version("1.0.0") // 版本 // 姓名,聯(lián)系 link,郵箱 .contact(new Contact("Jimmy", "https://juejin.cn/user/1996368846261294", "reng99@outlook.com")) .build(); } }
到此,我們運(yùn)行項(xiàng)目,打開連接 http://localhost:8080/swagger-ui/index.html
,咦, 404
耶~
處理 404
版本的問題,使得我們無法讀取 swagger
包下面的頁面。那么,我們來重寫。
我們?cè)?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/"); } }
重新啟動(dòng),訪問路徑 http://localhost:8080/doc.html
,就可以看到效果。
在本文淺聊一下Spring Security的使用方法 中,我們已經(jīng)開發(fā)好了六個(gè)接口。點(diǎn)擊進(jìn)入其中一個(gè),比如 queryAll
查詢所有用戶的接口,可看到其文檔:
我們還可以對(duì)該接口進(jìn)行調(diào)試:
感興趣的讀者可以自行嘗試。
以上就是SpringBoot整合Swagger接口文檔工具的流程步驟的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合Swagger的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Springboot整合Swagger3全注解配置(springdoc-openapi-ui)
- SpringBoot整合Swagger3生成接口文檔過程解析
- Springboot整合Swagger2后訪問swagger-ui.html 404報(bào)錯(cuò)問題解決方案
- SpringBoot整合Swagger3生成接口文檔的示例代碼
- Springboot整合Swagger2和Swagger3全過程
- springboot整合swagger3和knife4j的詳細(xì)過程
- SpringBoot整合Swagger的方法示例
- SpringBoot整合swagger的操作指南
- SpringBoot整合Swagger教程詳解
- SpringBoot3.x整合swagger的實(shí)現(xiàn)示例
相關(guān)文章
使用Java實(shí)現(xiàn)讀取手機(jī)文件名稱
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)讀取手機(jī)文件名稱,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03Java反射之靜態(tài)加載和動(dòng)態(tài)加載的簡單實(shí)例
下面小編就為大家?guī)硪黄狫ava反射之靜態(tài)加載和動(dòng)態(tài)加載的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10SpringBoot項(xiàng)目配置數(shù)據(jù)庫密碼加密相關(guān)代碼
這篇文章主要介紹了SpringBoot項(xiàng)目配置數(shù)據(jù)庫密碼加密的相關(guān)資料,本文介紹了在Springboot項(xiàng)目中配置數(shù)據(jù)庫連接時(shí)存在的安全問題,即用戶名和密碼以明文形式存儲(chǔ),容易泄露,提出了一種簡單的加密方案,需要的朋友可以參考下2024-11-11詳解eclipse中Maven工程使用Tomcat7以上插件的方法
本篇文章主要介紹了詳解eclipse中Maven工程使用Tomcat7以上插件的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12spring mvc中@RequestBody注解的作用說明
這篇文章主要介紹了spring mvc中@RequestBody注解的作用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08redis防止重復(fù)提交的實(shí)現(xiàn)示例
在開發(fā)中我們都需要處理重復(fù)提交的問題,本文主要介紹了redis防止重復(fù)提交的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06SpringBoot配置加載,各配置文件優(yōu)先級(jí)對(duì)比方式
這篇文章主要介紹了SpringBoot配置加載,各配置文件優(yōu)先級(jí)對(duì)比方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08