问题描述
我正在尝试让 Swagger UI 与 Spring Boot 1.2.1 一起使用.我按照 https://github.com/martypitt/swagger-springmvc 上的说明进行操作,然后添加@EnableSwagger
在我的 spring 配置中.
I am trying to get Swagger UI working with Spring Boot 1.2.1. I followed the instructions at https://github.com/martypitt/swagger-springmvc and I added @EnableSwagger
on my spring config.
我目前在访问 http://localhost:8080/api-docs
时返回 JSON,但没有好的 HTML.
I currently get back JSON when I go to http://localhost:8080/api-docs
but no nice HTML.
我正在使用 Maven 并添加了对 swagger-ui 的依赖:
I am using Maven and added the dependency on swagger-ui:
<dependency>
<groupId>org.ajar</groupId>
<artifactId>swagger-spring-mvc-ui</artifactId>
<version>0.4</version>
</dependency>
这是我的完整依赖列表:
This is my complete list of dependencies:
<dependencies>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.9.4</version>
</dependency>
<dependency>
<groupId>org.ajar</groupId>
<artifactId>swagger-spring-mvc-ui</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
我也试过 http://localhost:8080/docs/index.html
作为 URL,但这只是给出了白标错误页面"
I also tried http://localhost:8080/docs/index.html
as URL, but that just gives the "Whitelabel Error Page"
更新:
我在 Github 上创建了一个测试项目来说明问题:https://github.com/wimdeblauwe/springboot-swagger-test
I created a test project on Github to show the problem: https://github.com/wimdeblauwe/springboot-swagger-test
推荐答案
您的问题在于您的 SwaggerConfiguration
文件.您需要去掉 @EnableWebMvc
,因为这会导致默认的 Spring Boot 视图解析器被默认的SpringWebMvc"覆盖,后者以不同的方式提供静态内容.
Your problem lies in your SwaggerConfiguration
file. You need to take out @EnableWebMvc
, because this causes the default Spring Boot view resolver to be overwritten by the default 'SpringWebMvc' one which serves static content differently.
默认情况下,Spring Boot 将从以下任一目录提供静态内容:
By default, Spring Boot will serve static content from any of the following directories:
- /META-INF/resources/
- /resources/
- /静态/
- /公共/
包括 webjar.
我遇到了同样的问题,我在文档中找到了这个:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-auto-configuration
I had the same problem and I found this in the documentation: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-auto-configuration
如果您想完全控制 Spring MVC,您可以添加您自己的 @Configuration
,并用 @EnableWebMvc
注释.如果您想保留 Spring Boot MVC 功能,并且只想添加额外的 MVC 配置(拦截器、格式化程序、视图控制器等),您可以添加您自己的 @Bean
类型 WebMvcConfigurerAdapter
,但没有 @EnableWebMvc
.
我希望这会有所帮助.
这篇关于无法让 Swagger UI 与 Spring Boot 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!