我目前正在从servlet切换到Spring Webflux,并使用Spring Cloud Gateway
,并尝试编写GlobalFilter
。
我的旧代码中有一部分在过滤器链中执行有关请求的断言,并返回错误:
override fun doFilterInternal(request: HttpServletRequest, response: HttpServletResponse, filterChain: FilterChain) {
// various assertions on request
if (assertionsFailed) {
response.sendError(...)
...
}
我当前的代码结构如下:
@Bean
fun preFilter(): GlobalFilter {
return GlobalFilter { exchange, chain ->
Mono.create<Void> { sink ->
// perform some processing on exchange.request
...
// trying to accomplish something similar to this
exchange.sendError(statusCode, MyErrorModel(reason))
sink.success()
}.then()
}
}
在此阶段如何以类似方式正确返回错误?