最近使用最新的SpringBoot2.0集成Swagger2的時候遇到一個問題,集成之后打開Swagger頁面的時候出現404,后臺提示找不到swagger-ui的頁面。
于是我看了下項目依賴swagger的結構:
可以看到 swagger-ui.html 在META-INF/resources目錄下,所以我們需要手動的將靜態資源路徑指向這里,在java中配置為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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把默認的靜態資源路徑覆蓋了,手動設置的方式 * * @param registry */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { // 解決靜態資源無法訪問 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,實現addResourceHandlers方法,設置靜態資源可訪問。
設置完成后重啟項目,就可以通過 http://localhost:8080/swagger-ui.html 正常訪問了。
===== 2019.03.13更新 =====
有的同學說配置swagger后靜態資源目錄無法訪問,我自己試了下,確實訪問不了。原來的配置是:
1
2
3
4
5
|
@Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { // 解決swagger無法訪問 registry.addResourceHandler( "/**" ).addResourceLocations( "classpath:/META-INF/resources/" ).setCachePeriod( 0 ); } |
這里是將所有的請求都指向了META-INF/resources/目錄,顯然是不對的,會導致項目的其他靜態文件目錄無法正常訪問,于是做了修改:
1
2
3
4
5
6
7
8
9
10
11
12
|
@Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { // 解決靜態資源無法訪問 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的解決操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/xqnode/article/details/81382160