StringBoot集成swagger2之后访问链接http://localhost:8080/swagger-ui.html返回404.

集成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/”);

springboot 集成swagger2 404 无法访问

你可能感兴趣的:(java,SpringBoot,swagger2)