Spring Boot 2-从RestControler返回rx

时间:2018-07-08 16:27:08

标签: spring spring-mvc spring-boot rx-java hystrix

我试图从Spring RestController返回一个Observable,但没有成功。我的代码如下:

@RestController
public class HystrixCommentController {

    @GetMapping(value = "/com1/{id}")
    public Observable<Comment> getComment1(@PathVariable int id) {
        return Observable.just(new Comment());
    }
}

在邮递员中运行请求时,总是出现以下错误:

{
  "timestamp": "2018-07-08T16:07:36.809+0000",
  "status": 500,
  "error": "Internal Server Error",
  "message": "No converter found for return value of type: class rx.internal.util.ScalarSynchronousObservable",
  "path": "/com1/1"
}
  • SpringBoot 2还不支持rx。在RestController中可观察到 就像使用Mono / Flux一样?

  • 我需要手动将Observable转换为Mono / Flux吗?

致谢

注意:pom中包含spring-boot-starter-webflux

我尝试过这个:

@RestController
    public class HystrixCommentController {

        @GetMapping(value = "/com1/{id}", produces = "application/json")
            public Observable<Comment> getComment1(@PathVariable int id) {
            return Observable.just(new Comment());
        }

}

没有成功:

{
    "timestamp": "2018-07-08T18:21:42.918+0000",
    "status": 406,
    "error": "Not Acceptable",
    "message": "Could not find acceptable representation",
    "path": "/com1/1"
}

1 个答案:

答案 0 :(得分:2)

所以这里的问题是我的pom.xml中缺少依赖项:

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

感谢@Dovmo发现问题