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@EnableSwagger2public 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 PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.Thu Nov 24 19:57:13 CST 2016There was an unexpected error (type=Not Found, status=404).No message available
但是我新建一個項目重新配置卻沒有任何問題,於是想到自己的項目中肯定有哪些配置與swagger衝突了,
最後發現在application.properties 中把
spring.resources.static-locations=classpath:/static/
這一行註釋掉即可訪問了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。