Swagger with Spring Boot 2.0 导致 404 错误页面

新手上路,请多包涵

我正在尝试将我的 Spring Boot 版本 2.0.1.RELEASESwagger 集成。

从这篇 博文 看来,只需添加两个 Maven 依赖项就很容易,一切都应该可以正常工作。

所以我在 pom 中添加了以下依赖项:

 <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.0</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.8.0</version>
</dependency>

并创建了 SwaggerConfig bean:

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

在属性文件中,我在尝试使其工作时得到了这 3 个条目:

 spring.application.name=cat-service
management.server.servlet.context-path=/cat-service
server.servlet.contextPath=/cat-service

但最后,当访问

http://localhost:8080/cat-service/api/v2/api-docs

或 UI 页面位于

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

我收到 page not found 错误。

在 swagger github 页面中发现了这个问题, 在 stackoverflow 中发现了这个问题,但我无法更改我的 404 错误。

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

阅读 667
2 个回答

我能够让它与 Spring 引导版本一起工作 2.0.4.RELEASE这篇博 文:

我添加了这些依赖项:

 <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

这个配置文件:

 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 SpringFoxConfig {
    @Bean
    public Docket apiDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

它奏效了。

可以在 /swagger-ui.html 访问 Swagger UI#

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

请查看参考: https ://springfox.github.io/springfox/docs/current/

“2.1.3. 从现有的 2.x 版本迁移”

您可以从 pom.xml 中删除 springfox-swagger2 和 springfox-swagger-ui 并改为添加 springfox-boot-starter(例如版本 3.0.0)。您也可以删除 @EnableSwagger2 注释

并且:“ swagger-ui 位置已从 https://host/context-path/swagger-ui.html 移动 https://host/context-path/swagger-ui/index.htmlhttps://host/ context-path/swagger-ui/ 简称。这使得将其作为 Web jar 拉出并在不需要时使用配置属性将其关闭时效果更好。”

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

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