【发布时间】:2020-07-26 16:13:06
【问题描述】:
有没有办法使用 Spring WebFlux 和默认的 Reactor Netty 在@ExceptionHandler 方法中访问RequestBody(最好是映射形式)?
考虑以下示例:
@RestController
class TestRestController {
@PostMapping("/test")
Mono<TestBody> testPost(@RequestBody TestBody testBody) {
return Mono.error(new NullPointerException());
}
@ExceptionHandler(NullPointerException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
Mono<Void> handleNullPointerException(ServerWebExchange serverWebExchange) {
return Mono.empty();
}
}
在运行时,certain types 的其他实例可以注入到@ExceptionHandler 的方法签名中,如上面的ServerWebExchange 示例所示。但是文档明确指出它不支持请求正文参数(请参阅this section 中的注释)。
使用 Servlet 堆栈,您可以将RequestContext 注入为shown here。 WebFlux 堆栈是否有等效或类似的方法?
【问题讨论】:
标签: java spring spring-boot spring-webflux