转载于:https://blog.csdn.net/hwangfantasy/article/details/66542602
SpringBoot 配置SwaggerUI 访问404的小坑
在学习SpringBoot构建Restful API的时候遇到了一个小坑,配置Swagger UI的时候无法访问。
首先在自己的pom文件中加入Swagger的依赖,如下所示:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
然后在新建一个SwaggerConfig类:
Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.nightowl"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("NightOwl RESTful APIs")
.description("关注我 http://hwangfantasy.github.io/")
.termsOfServiceUrl("http://hwangfantasy.github.io/")
.contact("颜艺学长")
.version("1.0")
.build();
}
}
最后在自己的Controller中加上一系列的API注解即可,其实不需要加上API注解也可以正常使用。
最后在localhost:8080/swagger-ui.html 访问即可看到swagger页面了。
但是关键来了,我第一次按照这样的方法配置却提示如下错误:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Nov 24 19:57:13 CST 2016
There was an unexpected error (type=Not Found, status=404).
No message available
但是我新建一个项目重新配置却没有任何问题,于是想到自己的项目中肯定有哪些配置与swagger冲突了,
最后发现在 application.properties 中把
spring.resources.static-locations=classpath:/static/
这一行注释掉即可访问了。
补充第二种方法:
看了下项目依赖swagger的结构:
可以看到 swagger-ui.html 在META-INF/resources目录下,所以我们需要手动的将静态资源路径指向这里,在java中配置为:
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 正常访问了。