SpringBoot 配置SwaggerUI 訪問404的小坑。
在學習SpringBoot構建Restful API的時候遇到了一個小坑,配置Swagger UI的時候無法訪問。
首先在自己的pom文件中加入Swagger的依賴,如下所示:
1
2
3
4
5
6
7
8
9
10
11
|
<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類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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頁面了。
但是關鍵來了,我第一次按照這樣的方法配置卻提示如下錯誤:
1
2
3
4
5
6
7
|
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 中把
1
|
spring.resources. static -locations=classpath:/ static / |
這一行注釋掉即可訪問了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/hwangfantasy/article/details/66542602