Spring Webflux 和 Observable 响应不工作
Spring Webflux and Observable responses not working
我刚刚使用 spring-boot-starter-webflux 2.0 版创建了一个简单的 Spring 引导应用程序。0.BUILD-SNAPSHOT 带来了 spring-webflux 版本5.0.0.BUILD-SNAPSHOT 与 Spring Core、Bean、Context 等相同
如果我创建一个简单的 @RestController
并提供一个简单 returns 和 Flux<String>
的 @GetMapping
,那么一切都会按预期进行。
但是,如果我从 Flux
更改为 RxJava 的 Observable
,我会得到这个错误:
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
通过代码调试了一下,我发现 Jackson 的 ObjectMapper
以某种方式在其 typeFactory 中注册了 Flux
、Mono
和其他反应类型,所以后来 MappingJackson2HttpMessageConverter
知道如何(反)序列化它们。
但是,当我使用 Observable
时情况并非如此:我没有在 ObjectMapper
的类型中找到注册的类型 Observable
或 Single
工厂,所以我得到了上述错误。
有人遇到过这个问题吗?我是否缺少任何依赖性?我是否需要手动告诉 Jackson 如何从 RxJava 构造中(反)序列化?但是为什么 Jackson 已经知道 Flux 和 Mono?
感谢您的帮助。
已编辑:
我正在使用 RxJava 1.2.7。这是我的 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>
这是我的控制器代码示例:
/**
* 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));
}
所以,Flux
和 Mono
是 Spring 的名字,因为 Spring 有 Not-Invented-Here 综合症。
您可以注册一个处理程序来转换为 Spring 知道的类型,使用 Jersey 提供的 AsyncResponse 工具,或者在任何地方添加 toBlocking。
您可能缺少以下依赖项才能使其正常工作:
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava-reactive-streams</artifactId>
<version>1.2.1</version>
</dependency>
各种 RxJava 1.x 版本的变化让我们很难立即支持它,这就是为什么我们更愿意依赖 official RxJava -> Reactive Streams adapter。请注意,支持 RxJava 2.x 不需要额外的依赖项(它原生地构建在 Reactive Streams 之上)。
我将更新 Spring WebFlux reference documentation 以指定这是获得 RxJava 1.x 支持所必需的。
我刚刚使用 spring-boot-starter-webflux 2.0 版创建了一个简单的 Spring 引导应用程序。0.BUILD-SNAPSHOT 带来了 spring-webflux 版本5.0.0.BUILD-SNAPSHOT 与 Spring Core、Bean、Context 等相同
如果我创建一个简单的 @RestController
并提供一个简单 returns 和 Flux<String>
的 @GetMapping
,那么一切都会按预期进行。
但是,如果我从 Flux
更改为 RxJava 的 Observable
,我会得到这个错误:
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
通过代码调试了一下,我发现 Jackson 的 ObjectMapper
以某种方式在其 typeFactory 中注册了 Flux
、Mono
和其他反应类型,所以后来 MappingJackson2HttpMessageConverter
知道如何(反)序列化它们。
但是,当我使用 Observable
时情况并非如此:我没有在 ObjectMapper
的类型中找到注册的类型 Observable
或 Single
工厂,所以我得到了上述错误。
有人遇到过这个问题吗?我是否缺少任何依赖性?我是否需要手动告诉 Jackson 如何从 RxJava 构造中(反)序列化?但是为什么 Jackson 已经知道 Flux 和 Mono?
感谢您的帮助。
已编辑:
我正在使用 RxJava 1.2.7。这是我的 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>
这是我的控制器代码示例:
/**
* 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));
}
所以,Flux
和 Mono
是 Spring 的名字,因为 Spring 有 Not-Invented-Here 综合症。
您可以注册一个处理程序来转换为 Spring 知道的类型,使用 Jersey 提供的 AsyncResponse 工具,或者在任何地方添加 toBlocking。
您可能缺少以下依赖项才能使其正常工作:
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava-reactive-streams</artifactId>
<version>1.2.1</version>
</dependency>
各种 RxJava 1.x 版本的变化让我们很难立即支持它,这就是为什么我们更愿意依赖 official RxJava -> Reactive Streams adapter。请注意,支持 RxJava 2.x 不需要额外的依赖项(它原生地构建在 Reactive Streams 之上)。
我将更新 Spring WebFlux reference documentation 以指定这是获得 RxJava 1.x 支持所必需的。