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行代码,搞定

 

 

posted @ 2017-07-21 15:48  moncat  阅读(30407)  评论(4编辑  收藏  举报