Spring webflux 错误处理程序:如何在错误处理程序中获取请求的反应器上下文?

2023-11-24

Spring boot 2.1.5 项目 Reactor 3.2.9

在我的 webflux 项目中,我广泛使用反应器上下文来传递一些值。

我的目的是能够获取异常处理程序内部的上下文。

一个简单的例子:

@Component
@Order(-2)
public class GlobalErrorWebExceptionHandler extends
    AbstractErrorWebExceptionHandler {

    public GlobalErrorWebExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties, ApplicationContext applicationContext, ServerCodecConfigurer configurer) {
        super(errorAttributes, resourceProperties, applicationContext);
        this.setMessageWriters(configurer.getWriters());
    }

    @Override
    protected RouterFunction<ServerResponse> getRoutingFunction(
      ErrorAttributes errorAttributes) {
        return RouterFunctions
            .route(RequestPredicates.all(), request -> {
                Throwable error = errorAttributes.getError(request);

                return ServerResponse.status(500).syncBody(error.getMessage()).doOnEach(serverResponseSignal -> {
                    //Here the context is empty because i guess i created a new flux
                    System.out.println("What is in my context ? " + serverResponseSignal.getContext());
                    System.out.println("What is my exception ? " + error);
                });
            });
    }

}

我不确定如何使用反应堆以干净的方式实现该目标。有人有想法吗?


我找到了一个能够实现这一目标的技巧。听起来不太干净,但似乎有效。

在过滤器中,我将订阅的上下文保留到请求属性中:

@Component
public class MdcWebFilter implements WebFilter {

    @NotNull
    @Override
    public Mono<Void> filter(@NotNull ServerWebExchange serverWebExchange,
                             WebFilterChain webFilterChain) {

        Mono<Void> filter = webFilterChain.filter(serverWebExchange);
        return filter
            .subscriberContext((context) -> {
                //This code is executed before the query

                Context contextTmp = context.put("whatever", "whichever");

                //I save the context in an attribute attribute
                serverWebExchange.getAttributes().put("context", contextTmp);

                return contextTmp;
            });
    }
}

然后就可以从反应式错误处理程序中获取它:

@Component
@Order(-2)
public class GlobalErrorWebExceptionHandler extends
    AbstractErrorWebExceptionHandler {

    public GlobalErrorWebExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties, ApplicationContext applicationContext, ServerCodecConfigurer configurer) {
        super(errorAttributes, resourceProperties, applicationContext);
        this.setMessageWriters(configurer.getWriters());
    }

    @Override
    protected RouterFunction<ServerResponse> getRoutingFunction(
      ErrorAttributes errorAttributes) {
        return RouterFunctions
            .route(RequestPredicates.all(), request -> {
                Throwable error = errorAttributes.getError(request);

                //The context will be visible in the whole error handling flow
                return ServerResponse.status(500).syncBody(error.getMessage())
                   .subscriberContext((Context) request.attribute("context").orElse(Context.empty())));
            });
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring webflux 错误处理程序:如何在错误处理程序中获取请求的反应器上下文? 的相关文章

随机推荐