【发布时间】:2018-02-08 14:26:34
【问题描述】:
我试图在 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 做到这一点吗?欢迎所有想法。谢谢。
【问题讨论】:
标签: java spring spring-boot spring-webflux