swagger-ui 和 spring webflux 的 404 错误 [英] 404 error with swagger-ui and spring webflux

查看:98
本文介绍了swagger-ui 和 spring webflux 的 404 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Webflux 开发 REST 服务,并且我想使用 Swagger2 为我的 API 生成文档.我发现只有 Swagger2 版本 3.0.0 快照支持 Webflux.

I am developping REST services with Spring Webflux and I want to produce documentation with Swagger2 for my API with. I discovered that Webflux is only supported with Swagger2 version 3.0.0 snapshot.

这是我的配置:

  • Java 11
  • Maven 3

我的 SwaggerConfiguration bean 看起来像这样

My SwaggerConfiguration bean looks like this

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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.EnableSwagger2WebFlux;

@Configuration
@EnableSwagger2WebFlux
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .description("My Reactive API")
                        .title("My Domain object API")
                        .version("1.0.0")
                        .build())
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.mypackage.service.myobject.controller"))
                .paths(PathSelectors.any())
                .build();

    }
}

我的 springboot 应用定义如下:

My springboot app is defined like this :

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.reactive.config.EnableWebFlux;

@SpringBootApplication
@EnableWebFlux
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

这是我的 pom 配置

Here's my pom configuration

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
</parent>

<properties>
    <swagger.version>3.0.0-SNAPSHOT</swagger.version>
</properties>

<dependencies>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${swagger.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-spring-webflux</artifactId>
        <version>${swagger.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-spring-integration-webflux</artifactId>
        <version>${swagger.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${swagger.version}</version>
    </dependency>
</dependencies>

但是,当我输入 http://localhost:8080/swagger-ui 时出现 404 错误.html 在我的浏览器中.如果我输入 http://localhost:8080/v2/api-docs,我会得到为我的 API 使用 json 格式的 swagger 文档.

However, I get a 404 error when I type http://localhost:8080/swagger-ui.html in my browser. If I type http://localhost:8080/v2/api-docs, I get the swagger document in json for my API.

我在 Intellij 下的调试中运行我的 springboot 应用程序.我尝试在命令行中运行 fat jar:同样的问题.有人知道我的配置有什么问题吗?

I run my springboot app in debug under Intellij. I try to run the fat jar in command line: same issue. Does somebody know what is wrong with my configuration?

谢谢

推荐答案

找到了!

我必须像这样为 swagger-ui 注册一个 webflux 资源处理程序.

I have to register a webflux resource handler for swagger-ui like this.

import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;

@Configuration
public class WebfluxConfig implements WebFluxConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/swagger-ui.html**")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

问题解决了!

这篇关于swagger-ui 和 spring webflux 的 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆