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沖突了,
最后發(fā)現(xiàn)在 application.properties 中把
spring.resources.static-locations=classpath:/static/
這一行注釋掉即可訪問了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
IntelliJ IDEA設置代碼的快捷編輯模板Live Templates
今天小編就為大家分享一篇關于IntelliJ IDEA設置代碼的快捷編輯模板Live Templates,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
Java ThreadPoolExecutor 線程池的使用介紹
Executors 是一個Java中的工具類. 提供工廠方法來創(chuàng)建不同類型的線程池,這篇文章主要介紹了Java ThreadPoolExecutor 線程池的使用介紹,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04

