我试图在Spring WebFlux中重新创建 HttpServletResponse#getWriter
功能但没有成功 .
在Spring MVC中,我会扩展 HandlerInterceptorAdapter
,使用提到的 getWriter
来编写目标响应并从 preHandle
方法返回false . 到目前为止,我没有成功 .
@Component
public class CustomInterceptor extends RequestMappingHandlerAdapter
@Override
public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
exchange.getResponse().writeWith(s -> Mono.just(new SomeDto()));
// creates Internal Server Error, custom WebExceptionHandler not called
return Mono.error(new RuntimeException());
// creates Internal Server Error, custom WebExceptionHandler not called
throw new RuntimeException();
// ignores response write
return super.handle(exchange, handler);
}
}
我也试过使用 WebFilter
,有可能改变响应,但是不可能访问 HandlerMethod
对象,这是进一步处理所需要的 .
知道如何使用Spring WebFlux做到这一点吗?欢迎所有想法 . 谢谢 .
1 回答
可以在新的反应
WebFilter
中处理请求/响应,例如 .检查我的完整webfilter sample .