添加了 Springfox Swagger-UI,但它不起作用,我错过了什么?

新手上路,请多包涵

按照此处的说明进行操作:

http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

我将这些依赖项添加到我的项目中:

 compile "io.springfox:springfox-swagger2:2.7.0"
compile "io.springfox:springfox-swagger-ui:2.7.0"

并像这样配置 SpringFox Swagger:

 import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

但 Swagger UI 似乎没有启用。我试过了:

我得到的是:

 Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Sep 11 09:43:46 BST 2017
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported

在我看到的日志上:

 2017-09-11 09:54:31.020  WARN 15688 --- [nio-8080-exec-6] o.s.web.servlet.PageNotFound             : Request method 'GET' not supported
2017-09-11 09:54:31.020  WARN 15688 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported

http://localhost:8080/swagger-resources 返回:

 [{"name": "default",
  "location": "/v2/api-docs",
  "swaggerVersion": "2.0"}]

我错过了什么?

原文由 pupeno 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
2 个回答

我遇到了这个问题,因为我有带有请求映射的端点,这些端点具有这种形式的路径变量:/{var}。事实证明,这对于 GET 和 POST 端点(即 GET /{var} 和 POST /{var} 块 swagger-ui)都是一个问题。一旦我使路径更具体,我就可以使用 swagger-ui 了。

引用自 https://github.com/springfox/springfox/issues/1672

当 spring 找到一个只有一个变量的简单路径时,swagger 无法拦截 URL。

通过调查评论中的各种想法发现。

原文由 StanislavL 发布,翻译遵循 CC BY-SA 3.0 许可协议

Springfox 3.0.0 仅适用于 Spring Boot <= 2.6.0-M2 但不适用于其以上版本

Spring Boot 2.6 M2 的选项 <

1. spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER #-> App.properties

- 但没有执行器

2.@EnableWebMvc

3. 迁移到 springdoc-openapi-ui - 与 io.springfox >= 3.X 相同的步骤

io.springfox >= 2.X

io.springfox >= 3.X

 <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.9.2</version>
 </dependency>
 <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.9.2</version>
 </dependency>
 <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-schema</artifactId>
   <version>2.9.2</version>
 </dependency>
 <dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-boot-starter</artifactId>
  <version>3.0.0</version>
 </dependency>

浏览器网址

http://localhost:8080/swagger-ui.html

浏览器网址

http://localhost:8080/swagger-ui/
 http://localhost:8080/swagger-ui/index.html

必须需要

mvn clean

必须需要

mvn clean

 @Configuration
 @EnableSwagger2
 @Configuration
 @EnableSwagger2

原文由 Ravi Parekh 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题