Spring Webflux 和 Observable 响应不起作用 [英] Spring Webflux and Observable responses not working

查看:55
本文介绍了Spring Webflux 和 Observable 响应不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用 spring-boot-starter-webflux 和版本 2.0.0.BUILD-SNAPSHOT 创建了一个简单的 Spring Boot 应用程序,它带来了 spring-webflux 版本 5.0.0.BUILD-SNAPSHOT 和 Spring Core、Beans、Context 相同等

I just created a simple Spring Boot application using spring-boot-starter-webflux with version 2.0.0.BUILD-SNAPSHOT which brings spring-webflux version 5.0.0.BUILD-SNAPSHOT and same for Spring Core, Beans, Context, etc.

如果我创建一个简单的 @RestController 并提供一个 @GetMapping 来简单地返回一个 Flux,那么一切都按预期进行.

If I create a simple @RestController and provide a @GetMapping that simply returns a Flux<String>, then everything works as expected.

但是,如果我从 Flux 更改为 RxJava 的 Observable,我会收到此错误:

However, if I change from Flux to RxJava's Observable, I get this error:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

通过代码调试一下,我发现 Jackson 的 ObjectMapper 以某种方式在其 typeFactory 中注册了 FluxMono 和其他反应类型,所以后来 MappingJackson2HttpMessageConverter 知道如何(反)序列化它们.

Debugging a bit through the code I found that Jackson's ObjectMapper somehow registers Flux, Mono and the rest of reactive types in its typeFactory, so later the MappingJackson2HttpMessageConverter knows how to (de)-serialize them.

但是,当我使用 Observable 时,情况并非如此:我没有在 ObservableSingle 中找到注册的类型 ObservableSinglecode>ObjectMapper 的类型工厂,所以我得到了上述错误.

However, it's not the case when I use an Observable: I don't find the type Observable or Single registered in the ObjectMapper's type factory, so I get the aforementioned error.

有人遇到过这个问题吗?我错过了任何依赖吗?我是否需要手动告诉 Jackson 如何从 RxJava 构造(反)序列化?但为什么 Jackson 已经知道 Flux 和 Mono?

Has anyone experienced this issue? Am I missing any dependency? Do I need to manually tell Jackson how to (de)-serialize from RxJava constructs? But why does Jackson already know about Flux and Mono?

感谢您的帮助.

我使用的是 RxJava 1.2.7.这是我的 pom.xml:

I'm using RxJava 1.2.7. Here is my pom.xml:

<dependencies>
    <!-- Spring Boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
    </dependency>
    <!-- RxJava dependeencies -->
    <dependency>
        <groupId>io.reactivex</groupId>
        <artifactId>rxjava</artifactId>
        <version>1.2.7</version>
    </dependency>
</dependencies>

这是我的控制器代码示例:

And here is the example of my controller code:

/**
 * Get all the available tasks.
 *
 * @return the list of tasks
 */
@GetMapping(path = "/task")
@ResponseStatus(HttpStatus.OK)
public Observable<TaskView> allTasks(@AuthenticationPrincipal LoggedUserVO principal) {
    return this.pocComponent.getAllTasks()
            .map(t -> ViewConverter.convertFromTaskDocument(t, principal));
}

推荐答案

您可能缺少以下依赖项才能使其正常工作:

You are probably missing the following dependency to make it works:

<dependency>
    <groupId>io.reactivex</groupId>
    <artifactId>rxjava-reactive-streams</artifactId>
    <version>1.2.1</version>
</dependency>

各种 RxJava 1.x 版本的变化让我们很难立即支持它,这就是为什么我们更喜欢依赖 官方 RxJava -> Reactive Streams 适配器.请注意,无需额外依赖即可支持 RxJava 2.x(它原生构建在 Reactive Streams 之上).

Changes in various RxJava 1.x versions made it challenging for us to support it out of the box, that's why we prefer to rely on the official RxJava -> Reactive Streams adapter. Notice that RxJava 2.x is supported without requiring additional dependencies (it is natively built on top of Reactive Streams).

我要更新Spring WebFlux 参考文档 指定这是需要 RxJava 1.x 支持.

I am going to update Spring WebFlux reference documentation to specify this is required to have RxJava 1.x support.

这篇关于Spring Webflux 和 Observable 响应不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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