成都网站建设设计

将想法与焦点和您一起共享

SpringBoot如何解决配置SwaggerUI访问404错误的问题-创新互联

这篇文章将为大家详细讲解有关SpringBoot如何解决配置SwaggerUI访问404错误的问题,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

成都创新互联公司成立与2013年,先为山城等服务建站,山城等地企业,进行企业商务咨询服务。为山城企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

SpringBoot 配置SwaggerUI 访问404的小坑。

在学习SpringBoot构建Restful API的时候遇到了一个小坑,配置Swagger UI的时候无法访问。

首先在自己的pom文件中加入Swagger的依赖,如下所示:


      io.springfox
      springfox-swagger-ui
      2.2.2
    

    
      io.springfox
      springfox-swagger2
      2.2.2

然后在新建一个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/

这一行注释掉即可访问了。

SpringBoot如何解决配置SwaggerUI访问404错误的问题

关于“SpringBoot如何解决配置SwaggerUI访问404错误的问题”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


当前题目:SpringBoot如何解决配置SwaggerUI访问404错误的问题-创新互联
当前URL:http://chengdu.cdxwcx.cn/article/hgdgc.html