我想使用handle
运算符,但它的结果不是我期望的类型,它始终是Object
Mono.just("lol").handle((string, sink) -> {
if(!string.equals("lol")) {
sink.error(new RuntimeException("not lol!"));
} else {
sink.next(2);
}
}).doOnNext(myInt -> { // expecting myInt to be an integer but is Object
System.out.println(myInt);
});
我怎样才能得到识别类型的句柄(类似于 map
或 flatMap
识别返回类型的方式)?
我是否总是必须使用 cast
运算符?
最佳答案
使用泛型。
Mono.<String>just("lol").<Integer>handle((string, sink) -> {
if(!string.equals("lol")) {
sink.error(new RuntimeException("not lol!"));
} else {
sink.next(2);
}
}).doOnNext(myInt -> {
System.out.println(myInt);
})
关于spring-webflux - react 堆句柄运算符返回对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63818860/