之前按照网上的教程集成swagger2.集成之后访问链接http://localhost:8080/swagger-ui.html返回404.
然后看了一下错误日志,报下面的错误:
2018-06-06 11:26:06.903 WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/swagger-ui.html] in DispatcherServlet with name ‘dispatcherServlet’
我配置了全局的404处理,如果没有配置,可能报错不一样.原来是资源找不到导致的.其实只要手动加上静态资源的映射就OK啦
如何解决:
手动配置静态资源映射:
我使用的是spring2.x
@Configuration
public class WebConf extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//将所有/static/ 访问都映射到classpath:/static/ 目录下
registry.addResourceHandler(“/static/”).addResourceLocations(“classpath:/static/”);
//swagger2
registry.addResourceHandler(“/swagger-ui.html”).addResourceLocations(“classpath:/META-INF/resources/”);
registry.addResourceHandler(“/webjars/
”).addResourceLocations(“classpath:/META-INF/resources/webjars/”);
super.addResourceHandlers(registry);
}
}
如果使用的是1.X的springboot,继承WebMvcConfigurerAdapter.
最主要就是这两句
讯享网
讯享网registry.addResourceHandler(“/swagger-ui.html”).addResourceLocations(“classpath:/META-INF/resources/”);
registry.addResourceHandler(“/webjars/”).addResourceLocations(“classpath:/META-INF/resources/webjars/”);

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/196933.html