从 Flux<String> 中获取字符串抛出类型不匹配:无法从一次性转换为字符串 [英] To take a String from a Flux&lt;String&gt; throws type mismatch: Cannot convert from Disposable to String

查看:135
本文介绍了从 Flux<String> 中获取字符串抛出类型不匹配:无法从一次性转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于

我做错了什么?

澄清一下,它用于测试 - 我需要一个内部字符串值.所以如果我有一个 Flux 字符串:one, two, three 那么我想设置另一个字符串值 one

字符串 s = ...

如果我尝试

String s = rates.doOnNext(System.out::print);

然后我得到一个 type mismatch: cannot convert from Flux到字符串

如果我尝试

String s = next().flatMap(System.out::print);

然后我得到一个 type mismatch: cannot convert from Mono到字符串

解决方案

我做错了什么?

目前尚不清楚您要实现的目标,但可能有以下几点:

  • A Flux 返回 0..n 个字符串的流,您是否肯定只想要它返回的第一个字符串(这就是 next()是吗?)
  • 从发布者那里获取值实际上有两种选择 - 您可以使用反应链异步执行此操作,或者您可以阻塞线程直到它完成,然后直接获取它.(请注意,如果您阻止,则会失去响应式框架为您提供的所有优势,因此这实际上是您想要做的事情很少见.)在此示例中,您看起来像是在订阅,但是 试图阻止,这没有任何意义 - 你需要选择一个.
  • 如果你不想阻塞并且你正在使用像 webflux 这样的反应式框架,那么订阅通常是框架的工作 - 你只需将你的反应式调用传递到链上,直到你最终在(例如)您的控制器方法上返回了 MonoFlux.(订阅只有在您不使用框架并自己管理整个反应链 + 事件循环时才真正有效.)
  • 如果您不想阻止但想确保该值在发出后立即打印出来(称为副作用),那么您可以使用 doOnNext(System.out::print) 作为反应链的一部分来实现这一点.

Related to this question to take a string from a Mono I think I have finally got how to take a String out of a Flux<String> like this:

Flux<String> rates = controller.getRate(json);          
String myRate = rates.next().subscribe(System.out::print);

However, this is causing a type mismatch cannot convert from Disposable to String

What am I getting wrong?

To clarify, it's for testing - I need an internal String value. So if I have a Flux<String> of strings: one, two, three then I want to set another String to value one

String s = ...

If I try

String s = rates.doOnNext(System.out::print);

Then I get a type mismatch: cannot convert from Flux<String> to String

If I try

String s = next().flatMap(System.out::print);

Then I get a type mismatch: cannot convert from Mono<Object> to String

解决方案

What am I getting wrong?

It's not clear what you're trying to achieve, but potentially a few things here:

  • A Flux<String> returns a stream of 0..n strings, do you definitely only ever want the first one it returns (which is what next() does?)
  • You've realistically got two options in getting a value from a publisher - you can do it asynchronously by using the reactive chain, or you can block the thread until it completes, then get it directly. (Note that if you block though, you're losing all the advantages the reactive framework gives you, so it's rare that this is actually what you want to do.) In this example it looks like you're subscribing, but also trying to block, which doesn't make any sense - you need to pick one or the other.
  • If you don't want to block and you're using a reactive framework like webflux, then subscribing is generally the framework's job anyway - you would just pass your reactive calls up the chain until you eventually returned a Mono or a Flux on (for example) your controller method. (Subscribing is only really valid if you're not using a framework, and managing the entirety of the reactive chain + event loop yourself.)
  • If you don't want to block but want to make sure the value is printed out as soon as it's emitted (known as a side-effect) then you can use the doOnNext(System.out::print) as part of the reactive chain to achieve this.

这篇关于从 Flux<String> 中获取字符串抛出类型不匹配:无法从一次性转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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