使用 Spring Boot 2.0 的 Swagger 导致 404 错误页面

Swagger with Spring Boot 2.0 leads to 404 error page(使用 Spring Boot 2.0 的 Swagger 导致 404 错误页面)

本文介绍了使用 Spring Boot 2.0 的 Swagger 导致 404 错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的 Spring Boot 版本 2.0.1.RELEASE 与 Swagger.

I'm trying to integrate my Spring Boot version 2.0.1.RELEASE with Swagger.

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

From this blog post it seemed like it will be easy by just adding two Maven dependencies and everything should work.

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

So I added the following dependencies to the 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:

And created the 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 个条目:

And in the properties file I ended up with these 3 entries during the attempts to make it work:

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

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

我收到 page not found 错误.

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

I found this issues in the swagger github page and this question in stackoverflow but I was not able to change my 404 error.

推荐答案

我能够使它与 Spring boot 版本 2.0.4.RELEASE 和 这篇博文:

I was able to make it work with Spring boot version 2.0.4.RELEASE and this blog post:

我添加了这些依赖项:

<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 用户界面

The Swagger UI can be reached at /swagger-ui.html#

这篇关于使用 Spring Boot 2.0 的 Swagger 导致 404 错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 Spring Boot 2.0 的 Swagger 导致 404 错误页面

基础教程推荐