SpringBoot配置SwaggerUI访问404错误的解决⽅法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("关注我 hwangfantasy.github.io/")
.termsOfServiceUrl("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.
springboot其实就是spring
Thu Nov 24 19:57:13 CST 2016
There was an unexpected error (type=Not Found, status=404).
No message available
但是我新建⼀个项⽬重新配置却没有任何问题,于是想到⾃⼰的项⽬中肯定有哪些配置与swagger冲突了,
最后发现在 application.properties 中把
这⼀⾏注释掉即可访问了。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。