spring boot 2整合swagger-ui過程解析
這篇文章主要介紹了spring boot 2整合swagger-ui過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1.添加mvn依賴
修改pom.xml加入
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.5.0</version> </dependency>
2.創(chuàng)建配置類
在Application.java同級創(chuàng)建Swagger2的配置類Swagger2
package com.tydt.decision; 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 Swagger2{ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.tydt.decision.controller")) .paths(PathSelectors.any()).build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("Decision Manage Swagger RESTful APIs") .description("Decision API") .termsOfServiceUrl("http://swagger.io/") .contact(new Contact("Beibei", "127.0.0.1", "XXXXXXX@qq.com")) .version("1.0") .build(); } }
注:
如果出現(xiàn)下面情況
引入需要的包
但是啟動時(shí)又出現(xiàn)Error creating bean with name 'apiDocumentationScanner' defined in URL
這是由于swagger依賴google的guava,需要添加依賴,而當(dāng)前項(xiàng)目的guava版本與之不匹配,修改為
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>15.0</version> </dependency>
http://localhost:8090/swagger-ui.html頁面沒顯示
這是因?yàn)镾pring Boot自動配置本身不會自動把/swagger-ui.html這個(gè)路徑映射到對應(yīng)的目錄META-INF/resources/下面。加上這個(gè)映射即可
@Configuration public class WebMvcConfig implements WebMvcConfigurer { …… public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
解決了上面出現(xiàn)的問題,訪問http://localhost:8090/swagger-ui.html就能看到下面的頁面了
說明:
(1)通過@Configuration注解,讓Spring來加載該類配置
(2)再通過@EnableSwagger2注解來啟用Swagger2
(3)通過createRestApi函數(shù)創(chuàng)建Docket的Bean之后,apiInfo()用來創(chuàng)建該Api的基本信息
(4)select()函數(shù)返回一個(gè)ApiSelectorBuilder實(shí)例用來控制哪些接口暴露給Swagger來展現(xiàn)
(5)指定掃描的包路徑來定義,會掃描該包下所有Controller定義的API,并產(chǎn)生文檔內(nèi)容,除了用@ApiIgnore指定的
(6)通過@ApiOperation注解來給API增加說明
(7)通過@ApiImplicitParams
(8)@ApiImplicitParam注解來給參數(shù)增加說明
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Security整合KeyCloak保護(hù)Rest API實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Spring Security整合KeyCloak保護(hù)Rest API實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11java使用mysql預(yù)編譯語句查詢優(yōu)勢及示例詳解
這篇文章主要為大家介紹了java使用mysql預(yù)編譯語句的優(yōu)勢特點(diǎn)及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06springcloud gateway聚合swagger2的方法示例
這篇文章主要介紹了springcloud gateway聚合swagger2的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04java中關(guān)于getProperties方法的使用
這篇文章主要介紹了java中關(guān)于getProperties方法的使用,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12FasfDFS整合Java實(shí)現(xiàn)文件上傳下載功能實(shí)例詳解
這篇文章主要介紹了FasfDFS整合Java實(shí)現(xiàn)文件上傳下載功能實(shí)例詳解,需要的朋友可以參考下2017-08-08druid?handleException執(zhí)行流程源碼解析
這篇文章主要為大家介紹了druid?handleException執(zhí)行流程源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09