问题描述
我想使用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);
})