【问题标题】:Reactivestreams Subscriber not works with Spring Reactor Mono. Why?Reactivestreams 订阅者不适用于 Spring Reactor Mono。为什么?
【发布时间】:2025-12-20 22:05:07
【问题描述】:

我有一个 reactor.core.publisher.Mono 变量并想订阅 org.reactivestreams.Subscriber,尽管它似乎不起作用。 我无法理解为什么从未调用过 onNext 方法?我看到 onSubscribe 方法调用正常。我可能弄错了,但是作为 Mono 实现 Publisher,订阅者应该可以工作。对吧?

@Test
    public void subscriberTest() {
        Mono<String> m = Mono.just("Hello!");
        Subscriber<String> s = new Subscriber<String>() {
            @Override
            public void onSubscribe(Subscription s) {
                System.out.println("Subscription "+s);
            }
            @Override
            public void onNext(String t) {
                System.out.println("onNext "+t);
            }
            @Override
            public void onError(Throwable t) {
                System.out.println("Throwable "+t);
            }
            @Override
            public void onComplete() {
                System.out.println("onComplete");
            }
        };
        m.subscribe(s);

        Mono<String> m1 = Mono.just("Bye!");
        m1.subscribe(System.out::println);
    }

虽然带有方法引用的变量 m1 订阅工作正常。这里控制台输出:

Subscription reactor.core.publisher.StrictSubscriber@4b168fa9
Bye!

在这里我期待看到你好!短语也是。

【问题讨论】:

  • 好的,谢谢!如果你愿意,我会接受你的回答。

标签: java spring project-reactor reactive-streams


【解决方案1】:

【讨论】:

    最近更新 更多