【问题标题】:Unable to display Swagger/OpenApi docs when using SpringDoc webflux support使用 SpringDoc webflux 支持时无法显示 Swagger/OpenApi 文档
【发布时间】:2020-12-08 21:56:41
【问题描述】:

我有一个小型 Spring Boot 微服务,它使用 webflux 将其端点公开为反应式。

当我从 IntelliJ、Gradle 或带有 SpringDoc webflux 支持的 cmd 行运行应用程序时:
implementation 'org.springdoc:springdoc-openapi-webflux-ui:1.4.4'
然后我转到 http://localhost:8080/swagger-ui.html 我收到 500 错误,日志显示:

java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.http.server.reactive.ServerHttpRequest]

以及根本原因:

java.lang.NoSuchMethodException: org.springframework.http.server.reactive.ServerHttpRequest.<init>()

如果我使用http://localhost:8080/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config 我得到的是 404 而不是 500。

但是,如果我将依赖项更改为非反应性 SpringDoc 模块:
implementation 'org.springdoc:springdoc-openapi-ui:1.4.4'

文档可用,只是不显示返回架构(我猜这是意料之中的,因为响应包含在 Mono 中)。

I have looked at this question, but it didnt help

我使用的是 Spring Boot 2.3.3.RELEASE,除了自动装配的服务类之外,我在控制器中没有任何“花哨”的东西,端点只是用 GetMapping 注释并返回 Mono<T>。我已经包含了以下依赖项:

    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springdoc:springdoc-openapi-webflux-ui:1.4.4'

【问题讨论】:

    标签: spring-boot swagger spring-webflux springdoc


    【解决方案1】:

    您的项目正在使用 spring-boot-starter-web 和 spring-boot-starter-webflux。

    您只需从依赖项中删除 spring-boot-starter-web。

    【讨论】:

    • 啊,这有点问题,因为 Azure 应用程序洞察模块目前不支持 webflux(尽管看起来他们有可用的预览版),只要我能够升级 azure 模块我会重新审视这个。
    • 能够确认这将解决我在另一个项目上的问题。
    【解决方案2】:

    我有一个限制,我需要通过 Servlet 3.1 使用 Webflux,即使它不是纯反应式解决方案,但我可以使用 Servlet 重用一些 Spring 安全过滤器。所以,我不能只删除 spring-boot-starter-web 作为@brianbro 建议。

    因此,我找到了一个解决方案,通过添加来自 springdoc-openapi-webflux-core 的 WebFluxSupportConverter 作为额外的转换器,使 springdoc-openapi-ui 能够读取 Flux 和 Mono。请参阅 Groovy 中的以下代码。

        @Bean
        public OpenAPI customOpenAPI() {
    
            ModelConverters.getInstance().addConverter(new WebFluxSupportConverter())
    
            return new OpenAPI()
                .components(new Components()
                    .addSecuritySchemes("JWE", new SecurityScheme()
                        .type(SecurityScheme.Type.HTTP)
                        .scheme("bearer")
                        .bearerFormat("JWE")))
                .info(new Info()
                    .title("Marketplace API")
                    .version(apiVersion))
        }
    

    因此,您只需要包含库即可。

        implementation 'org.springdoc:springdoc-openapi-ui:1.4.6'
        implementation 'org.springdoc:springdoc-openapi-webflux-core:1.4.6'
    

    【讨论】:

    • 谢谢!。这对我的问题有用。我使用的是 sprng webflux 和 spring doc。最初我尝试在 pom.xml 文件 springdoc-openapi-ui 和 springdoc-openapi-webmvc-core 中添加以下依赖项,但它给了我一个错误。后来改成 springdoc-openapi-webflux-ui 和 springdoc-openapi-webflux-core 后,应用程序启动正常并且能够访问 swagger。
    【解决方案3】:

    对我来说,在 kotlin spring boot 项目中,问题在于端点返回 Flux,所以它抛出了一个优雅的异常:jackson BeanDescription.findJsonValueAccessor not found, this could lead to inaccurate result, please update jackson to 2.9+ @Chayne P.S. 的方法解决了我的问题:

    class OpenApiConfigiguration {
    
        @Bean
        fun configOpenAPI(): OpenAPI? {
            ModelConverters.getInstance().addConverter(WebFluxSupportConverter())
            return OpenAPI()
                    .info(Info().title("API")
                            .description("WS restful API")
                            .version("v0.0.1")
                            .license(License().name("License of API")
                            .termsOfService("Terms of service"))
                    .externalDocs(ExternalDocumentation()
                            .description("Docs")
                            .url(""))
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 2020-05-06
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 2020-04-02
      • 2020-05-19
      相关资源
      最近更新 更多