SpringBoot配置SwaggerUI訪問404錯誤


先引用一下別的仁兄的地址

http://blog.csdn.net/hwangfantasy/article/details/66542602   顏藝公社

上面這位仁兄說的也不夠清晰,在這里我補充一下。

先說明原因,出現404不是說文件沒有,而是映射出現了問題,特別是靜態文件映射。

這里親測成功,廢話不多說,直接上代碼

 1 package com.co.example.config;
 2 
 3 import org.springframework.context.annotation.Configuration;
 4 import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
 5 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 6 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
 7 import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
 8 
 9 import com.co.example.base.interceptor.BaseInterceptor;
10 /**
11  * 攔截器配置
12  * @author zyl
13  *
14  */
15 @Configuration
16 public class ServletContextConfig extends WebMvcConfigurationSupport {
17 
18     /**
19      * 發現如果繼承了WebMvcConfigurationSupport,則在yml中配置的相關內容會失效。
20      * 需要重新指定靜態資源
21      * @param registry
22      */
23     @Override
24     public void addResourceHandlers(ResourceHandlerRegistry registry) {
25         registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
26         registry.addResourceHandler("swagger-ui.html")
27         .addResourceLocations("classpath:/META-INF/resources/");
28         registry.addResourceHandler("/webjars/**")
29         .addResourceLocations("classpath:/META-INF/resources/webjars/");
30         super.addResourceHandlers(registry);
31     }
32 
33 
34     /**
35      * 配置servlet處理
36      */
37     @Override
38     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
39         configurer.enable();
40     }
41 
42 }

在本配置文件中加入第26-29行代碼,搞定

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM