首页 文章

添加了Springfox Swagger-UI并且它无法正常工作,我缺少什么?

提问于
浏览
2

按照这里的说明:

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"}]

我错过了什么?

1 回答

  • 4

    我遇到了这个问题,因为我的 endpoints 有请求映射,这些映射具有以下形式的路径变量:/ . 事实证明这对于GET和POST endpoints 都是一个问题,即GET / 和POST / 阻止swagger-ui . 一旦我把路径变得更加具体,我就会兴高采烈地工作 .

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

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

    通过调查评论中的各种想法找到 .

相关问题