如何在 Spring WebFlux 中设置 ViewResolver

How to set ViewResolver in Spring WebFlux

我的 Spring Boot Web Flux 应用程序已经有一段时间了。此时我想向我的路由器功能添加一些异常处理程序:

@Bean
RouterFunction<?> router(final GeneratorHandler generatorHandler) {
    return resources("/**", new ClassPathResource("/static/"))
            .andOther(route(GET("/generate"), generatorHandler::renderData)
                    .andRoute(GET("/index"), generatorHandler::renderIndex));
}

所以我像这样添加了另一个 bean:

@Bean
HttpHandler httpHandler(final GeneratorHandler generatorHandler) throws Exception {
    return WebHttpHandlerBuilder.webHandler(toHttpHandler(router(generatorHandler)))
            .prependExceptionHandler((serverWebExchange, exception) -> {
                //process here
                return null;
            })
            .build();
}

在此之后,我的视图解析器出现了问题。它找不到我的任何观点。经过调查,我意识到调试器不会在 ThymeleafReactiveViewResolver class 中停止。

是否有可能此更改也更改了默认视图解析器?我怎样才能把它带回来?

提供 httpHandler 会禁用 Spring Boot 的大量支持。

您可以将自己的 WebExceptionHandler 声明为组件(即使是订购的组件),Spring WebFlux 会为您挑选它。没有更具体的示例(或者至少是您看到的 stacktrace/error),很难理解发生了什么。

Spring Boot 现在支持 WebFlux 应用程序中的错误处理(请参阅 #8625),以防它执行您想要实现的操作。