推扬网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
推扬网 门户 经验分享 查看内容

SpringBoot配置SwaggerUI访问404错误的解决方法

2020-4-11 13:35| 发布者: admin| 查看: 403| 评论: 0

这篇文章主要为大家详细介绍了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("关注我 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/

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持


鲜花

握手

雷人

路过

鸡蛋

最新评论

精选推荐

    广告服务|投稿要求|禁言标准|版权说明|免责声明|手机版|小黑屋|推扬网 ( 粤ICP备18134897号 )|网站地图 | 邮箱:vayae@hotmail.com

    GMT+8, 2024-3-29 13:18 , Processed in 0.063266 second(s), 28 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    返回顶部