我有一个webflux Web应用程序。处理程序完成后,我正尝试转发到html页面。我的代码如下。这容易做到吗?
路由器
@Configuration
class WebRouter(val handler: Handler) {
@Bean
fun route() = router {
accept(MediaType.APPLICATION_JSON).nest {
GET("/fixing_check", handler::check_fixing).also { GET("/best", handler::anotherHandler) }
}
}
}
处理程序
fun check_fixing (serverRequest: ServerRequest): Mono<ServerResponse>{
.....
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(
Flux.fromStream(tocList.stream()).log(), FixingData::class.java
)
}
静态数据的配置
@Configuration
class WebConfiguration {
@Bean
fun resRouter() = resources("/**", ClassPathResource("static/"))
}
感谢您的帮助!
您可以使用then()
运算符在完成处理程序后更新路由器配置以进行重定向:
@Configuration
class WebRouter(val handler: Handler) {
@Bean
fun route() = router {
accept(MediaType.APPLICATION_JSON).nest {
GET("/fixing_check") { _ -> handler::check_fixing.then(ServerResponse.temporaryRedirect(URI.create("/best")).build()) } }
GET("/best") { _ -> handler::anotherHandler }
}
}
}
感谢上述更新。我用以下代码得出了结论:
@Component
class Handler {
val testList = listOf("String1", "String2", "String3")
fun checkSomething(severRequest: ServerRequest): Mono<ServerResponse> {
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(
Flux.fromIterable(testList), String::class.java
).then(ServerResponse.temporaryRedirect(URI("/test_thymeleaf.html")).build())
}
}
处理程序现在转发到html页面!太好了。
感谢您的帮助