Spring Reactive应用程序上的请求/响应正文的副本?
我正在研究访问HTTP请求和响应正文的最佳方法,以便在Spring反应性应用程序中进行跟踪。
对于以前的版本,我们利用Servlet筛选器和Servlet请求包装器来使用传入请求的输入流,并保留它的副本以用于跟踪的异步处理(我们将它们发送到Elasticearch)。
但是对于一个Spring Reactive应用程序(使用WebFlux),我想知道在请求被解码之前访问它们的最合适的方式是什么。有什么想法吗?
解决方案
事实证明,这可以使用提供的修饰符分别实现:ServerWebExchangeDecorator
、ServerHttpRequestDecorator
和ServerHttpResponseDecorator
。
下面是一个示例请求修饰符,它在请求的默认订阅者读取内容时累积DataBuffer
内容:
@Slf4j
public class CachingServerHttpRequestDecorator extends ServerHttpRequestDecorator {
@Getter
private final OffsetDateTime timestamp = OffsetDateTime.now();
private final StringBuilder cachedBody = new StringBuilder();
CachingServerHttpRequestDecorator(ServerHttpRequest delegate) {
super(delegate);
}
@Override
public Flux<DataBuffer> getBody() {
return super.getBody().doOnNext(this::cache);
}
@SneakyThrows
private void cache(DataBuffer buffer) {
cachedBody.append(UTF_8.decode(buffer.asByteBuffer())
.toString());
}
public String getCachedBody() {
return cachedBody.toString();
}
只需确保在修饰WebFilter
传递的ServerWebExchange
时,也重写getRequest()
以返回请求修饰符:
public final class PartnerServerWebExchangeDecorator extends ServerWebExchangeDecorator {
private final ServerHttpRequestDecorator requestDecorator;
private final ServerHttpResponseDecorator responseDecorator;
public PartnerServerWebExchangeDecorator(ServerWebExchange delegate) {
super(delegate);
this.requestDecorator = new PartnerServerHttpRequestDecorator(delegate.getRequest());
this.responseDecorator = new PartnerServerHttpResponseDecorator(delegate.getResponse());
}
@Override
public ServerHttpRequest getRequest() {
return requestDecorator;
}
@Override
public ServerHttpResponse getResponse() {
return responseDecorator;
}
}
在过滤器上:
@Component
public class TracingFilter implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(new PartnerServerWebExchangeDecorator(exchange));
}
}
可以这样使用的函数(注意静态导入的函数):
@Bean
public HttpHandler myRoute(MyHandler handler) {
final RouterFunction<ServerResponse> routerFunction =
route(POST("/myResource"), handler::persistNotification);
return webHandler(toWebHandler(routerFunction))
.filter(new TracingFilter())
.build();
}
相关文章