gpt4 book ai didi

spring-webflux - Spring webflux 错误处理程序 : How to get the reactor context of the request in the error handler?

转载 作者:行者123 更新时间:2023-12-04 04:32:31 26 4
gpt4 key购买 nike

Spring Boot 2.1.5 项目 react 堆 3.2.9

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

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

一个简单的例子:

@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);
});
});
}

}

我不确定如何使用 react 器以干净的方式实现该目标。任何人的想法?

最佳答案

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

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

@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;
});
}
}

然后可以从响应式(Reactive)错误处理程序中获取它:

@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())));
});
}

}

关于spring-webflux - Spring webflux 错误处理程序 : How to get the reactor context of the request in the error handler?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56577196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com