我必须提供api,它执行多个api调用,然后组合它们并返回单个
class MyDto {
private ItemA itemA;
private ItemB itemB;
private List<ItemC> itemCList;
//constructor
}
首先我得到mono,然后我将user传递给api调用,然后使用mono.zip组合响应,最后使用rxjava2adapter.monotosingle将响应转换为single
return RxJava2Adapter.monoToSingle(
userService.getUserById(id)
.flatMap(user -> Mono.zip(
getMonoItemA(user),
getMonoItemB(user),
getFluxItemC(user).collectList(),
.map(data -> new MyDto(data.getT3(), data.getT2(), data.getT1()))));
当我请求时,我的服务器就冻结了,我得到了一个错误:
io.reactivex.exceptions.CompositeException: 2 exceptions occurred.
at io.reactivex.internal.operators.single.SingleDoOnError$DoOnError.onError(SingleDoOnError.java:61)
at io.reactivex.internal.operators.single.SingleDoOnSuccess$DoOnSuccess.onSuccess(SingleDoOnSuccess.java:56)
at io.reactivex.internal.operators.single.SingleFromPublisher$ToSingleObserver.onComplete(SingleFromPublisher.java:101)
at reactor.core.publisher.StrictSubscriber.onComplete(StrictSubscriber.java:123)
at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1624)
Caused by: io.reactivex.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
at io.reactivex.exceptions.CompositeException.getCause(CompositeException.java:110)
at com.eisgroup.genesis.facade.FacadeErrorResponseFactory.lambda$resolveErrorStrategy$3(FacadeErrorResponseFactory.java:59)
at java.base/java.util.Optional.or(Optional.java:318)
at com.eisgroup.genesis.facade.FacadeErrorResponseFactory.resolveErrorStrategy(FacadeErrorResponseFactory.java:59)
at com.eisgroup.genesis.facade.FacadeErrorResponseFactory.createErrorResponse(FacadeErrorResponseFactory.java:48)
Caused by: java.lang.NullPointerException: null
at com.eisgroup.genesis.trace.config.RxAwareScope.close(RxAwareScope.java:34)
at com.eisgroup.genesis.facade.servlet.request.RequestDispatcher.invokeAfterInvocation(RequestDispatcher.java:314)
at com.eisgroup.genesis.facade.servlet.request.RequestDispatcher.lambda$invokeAfterInvocation$21(RequestDispatcher.java:301)
at io.reactivex.internal.operators.single.SingleDoOnSuccess$DoOnSuccess.onSuccess(SingleDoOnSuccess.java:53)
at io.reactivex.internal.operators.single.SingleFromPublisher$ToSingleObserver.onComplete(SingleFromPublisher.java:101)
Caused by: java.lang.NullPointerException: null
at com.eisgroup.genesis.trace.config.RxAwareScope.close(RxAwareScope.java:34)
at com.eisgroup.genesis.facade.servlet.request.RequestDispatcher.lambda$invokeAfterInvocation$23(RequestDispatcher.java:305)
at io.reactivex.internal.operators.single.SingleDoOnError$DoOnError.onError(SingleDoOnError.java:58)
at io.reactivex.internal.operators.single.SingleDoOnSuccess$DoOnSuccess.onSuccess(SingleDoOnSuccess.java:56)
at io.reactivex.internal.operators.single.SingleFromPublisher$ToSingleObserver.onComplete(SingleFromPublisher.java:101)
我尝试在出现错误或空响应的情况下添加默认返回(每个方法),但没有帮助
getFluxItemC(user).collectList()
.switchIfEmpty(Mono.defer(() -> Mono.just(new ArrayList<>())
.onErrorReturn(new ArrayList<>())
getItemA(user)
.switchIfEmpty(Mono.defer(() -> Mono.just(new ItemA)
.onErrorReturn(new ItemA)
rxjava新手,不知道如何处理此错误
1条答案
按热度按时间zpgglvta1#
在查看stacktrace时,很明显在#doonsuccess lambda中抛出了一个错误
io.reactivex.internal.operators.single.singledoonsuccess.doonsuccess
似乎调用了lamdba,抛出了一个异常,并将异常作为onerror传播到订阅服务器。
问题很可能发生在这里:
在com.eisgroup.genesis.trace.config.rxawarescope.close(rxawarescope。java:34)
你可以修复这个npe或者删除doonsuccess日志来运行它。
关于,为什么onerrorreturn不起作用。
事实上,如果没有适当的语境,很难说出来。我只能提供一些指导。#onerrorreturn运算符必须在抛出错误的运算符(doonsuccess)下面,以便正确处理错误。如果onerrorreturn在doonsucces运算符的上方,则onerror将传播到订阅服务器。
为了进一步帮助你,我需要更多的代码在里面,特别是facaderrorresponsefactory。
假设
monotosingle在onerrorreturn后添加#tosingle()#doonsuccess。因此,错误处理程序位于错误的位置,因此将异常转换为备份值。你能告诉我们monotosingle是如何实现的吗?