当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot配置SwaggerUI访问404错误的解决方法

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

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/

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网