[英]Spring WebFlux - statusCode is null in a WebFilter
当前使用Spring Boot 2.0.4开发的WebFilter在Spring WebFlux应用程序中记录HTTP请求和响应信息(URL,响应状态代码,持续时间等)。
除exchange.getResponse()。getStatusCode()保持为null之外,其他方法均能正常工作 。
尝试了两个不同的过滤器(ServerWebExchange交换,WebFilterChain链) ,第一个:
return chain.filter(exchange).doAfterTerminate(
() -> System.out.println("Status=" + exchange.getResponse().getStatusCode()));
第二个:
Mono<Void> filtered = chain.filter(exchange);
exchange.getResponse().beforeCommit(() -> {
System.out.println("Status=" + exchange.getResponse().getStatusCode());
return Mono.empty();
});
return filtered;
还尝试了过滤器上的各种订单:无,@ Order(100000),@ Order(-100000)。
状态代码保持为空。
什么是正确的实现?
更新1
编写了一个基于KevH解决方案的简约工作示例,请参阅https://github.com/fbeaufume/webflux-filter-sample,但它仍无法正常工作,状态仍然为null。 MWE公开了两个REST端点:/ hello和/ pause / 1000(暂停1秒)。
请注意,当调用暂停端点日志时:
11:06:20.644 INFO 9536 --- [ctor-http-nio-2] com.adeliosys.sample.LogFilter : Request [/pause/1000] started, traceId [bb3fe67d-170b-4070-837d-816fe1420a1f]
11:06:20.673 INFO 9536 --- [ctor-http-nio-2] com.adeliosys.sample.HelloController : Pausing for 1000 msec
11:06:21.683 INFO 9536 --- [ parallel-2] com.adeliosys.sample.LogFilter : Request [/pause/1000] completed, statusCode [null], time [1039], traceId [bb3fe67d-170b-4070-837d-816fe1420a1f]
11:06:21.684 INFO 9536 --- [ parallel-2] com.adeliosys.sample.HelloController : Paused for 1000 msec
我很惊讶过滤器的第二条消息显示在端点的第二条消息之前 。
更新2
看来使用doAfterTerminate(或类似方法)的过滤器实现是正确的,但这只能使用在返回类型中使用ResponseEntity的 REST控制器方法来正确检索HTTP响应状态。
不支持(即状态为null): void
, Mono<Void>
, String
, Mono<String>
, MyBean
, Mono<MyBean>
等。
支持(即状态为200): ReponseEntity<Void>
, Mono<ResponseEntity<Void>>
, ReponseEntity<String>
, Mono<ResponseEntity<String>>
, ReponseEntity<MyBean>
, Mono<ResponseEntity<MyBean>>
等。
错误?
Spring Boot 2.0.5具有相同的行为。
更新3
打开了关于该主题的问题,请参阅https://jira.spring.io/browse/SPR-17368
您可以尝试doAfterSuccessOrError
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
String traceId = UUID.randomUUID().toString();
long startTime = System.currentTimeMillis();
String path = exchange.getRequest().getURI().getPath();
System.out.printf("Request[%s] started, trace_id[%s]", path, traceId);
return chain.filter(exchange)
.doAfterSuccessOrError((r, t) -> {
System.out.printf("Request[%s], completed, status_code[%s], time[%d], trace_id[%s]", path,
exchange.getResponse().getStatusCode(), System.currentTimeMillis() - startTime, traceId);
})
.subscriberContext(Context.of(String.class, traceId));
}
样本输出
请求[/ logrequest]已开始,trace_id [b45b550a-b9ad-4323-a850-cb085a78a086]请求[/ logrequest],已完成,状态码[202],时间[41],trace_id [b45b550a-b9ad-4323-a850-cb085a78a086]
编辑:不确定为什么这不起作用,但是这两个解决方法都有一个ResponseEntity
@GetMapping("/hello")
public Mono<ResponseEntity<String>> publicHello() {
return Mono.just(ResponseEntity.ok("Hello world!"));
}
@GetMapping("/pause/{duration}")
@ResponseStatus(HttpStatus.ACCEPTED)
public Mono<Void> pause2(@PathVariable long duration) {
LOGGER.info("Pausing for {} msec", duration);
return (duration > 0 ? Mono.delay(Duration.ofMillis(duration)) : Mono.empty())
.then()
.doFinally(s -> LOGGER.info("Paused for {} msec", duration));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.