SpringBoot2.0集成Swagger2訪問404的解決操作
最近使用最新的SpringBoot2.0集成Swagger2的時候遇到一個問題,集成之后打開Swagger頁面的時候出現(xiàn)404,后臺提示找不到swagger-ui的頁面。
于是我看了下項目依賴swagger的結(jié)構(gòu):
可以看到 swagger-ui.html 在META-INF/resources目錄下,所以我們需要手動的將靜態(tài)資源路徑指向這里,在java中配置為:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; 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; /** * @author xiaqing */ @Configuration @EnableSwagger2 public class SwaggerConfig extends WebMvcConfigurationSupport { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.xqnode.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("接口總覽") .description("測試") .version("1.0") .build(); } /** * 防止@EnableMvc把默認的靜態(tài)資源路徑覆蓋了,手動設(shè)置的方式 * * @param registry */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { // 解決靜態(tài)資源無法訪問 registry.addResourceHandler("/**").addResourceLocations("classpath:/static/"); // 解決swagger無法訪問 registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); // 解決swagger的js文件無法訪問 registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
在swagger的配置類中繼承WebMvcConfigurationSupport,實現(xiàn)addResourceHandlers方法,設(shè)置靜態(tài)資源可訪問。
設(shè)置完成后重啟項目,就可以通過 http://localhost:8080/swagger-ui.html 正常訪問了。
===== 2019.03.13更新 =====
有的同學(xué)說配置swagger后靜態(tài)資源目錄無法訪問,我自己試了下,確實訪問不了。原來的配置是:
@Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { // 解決swagger無法訪問 registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0); }
這里是將所有的請求都指向了META-INF/resources/目錄,顯然是不對的,會導(dǎo)致項目的其他靜態(tài)文件目錄無法正常訪問,于是做了修改:
@Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { // 解決靜態(tài)資源無法訪問 registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/"); // 解決swagger無法訪問 registry.addResourceHandler("/swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); // 解決swagger的js文件無法訪問 registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); }
測試一下:
在resource的static文件夾下新建index.html
啟動項目訪問 http://localhost:8080/index.html
訪問正常,接下來再訪問swagger:
也是正常的。
以上這篇SpringBoot2.0集成Swagger2訪問404的解決操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis中 mapper-locations和@MapperScan的作用
這篇文章主要介紹了Mybatis中 mapper-locations和@MapperScan的作用,mybatis.mapper-locations在SpringBoot配置文件中使用,作用是掃描Mapper接口對應(yīng)的XML文件,需要的朋友可以參考下2023-05-05java封裝空值建議使用Optional替代null的方法示例解析
這篇文章主要為大家介紹了java封裝空值建議使用Optional替代null的方法原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11Spring Boot使用Thymeleaf + Gradle構(gòu)建war到Tomcat
今天小編就為大家分享一篇關(guān)于Spring Boot使用Thymeleaf + Gradle構(gòu)建war到Tomcat,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12spring-shiro權(quán)限控制realm實戰(zhàn)教程
這篇文章主要介紹了spring-shiro權(quán)限控制realm實戰(zhàn)教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10IntelliJ IDEA中properties文件顯示亂碼問題的解決辦法
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中properties文件顯示亂碼問題的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10Java?CompletableFuture實現(xiàn)原理分析詳解
CompletableFuture是Java8并發(fā)新特性,本文我們主要來聊一聊CompletableFuture的回調(diào)功能以及異步工作原理是如何實現(xiàn)的,需要的可以了解一下2022-09-09