Kotlin 如何从WebFlux处理程序返回HTML页面
我有一个webflux web应用程序。我试图在处理程序完成后转发到html页面。我的代码如下。这容易吗 路由器Kotlin 如何从WebFlux处理程序返回HTML页面,kotlin,routing,spring-webflux,Kotlin,Routing,Spring Webflux,我有一个webflux web应用程序。我试图在处理程序完成后转发到html页面。我的代码如下。这容易吗 路由器 @Configuration class WebRouter(val handler: Handler) { @Bean fun route() = router { accept(MediaType.APPLICATION_JSON).nest { GET("/fixing_check", handler::check_fixing
@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
)
}
谢谢你的帮助 您可以在完成处理程序后使用
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())
}
}
@组件
类处理程序{
val testList=listOf(“String1”、“String2”、“String3”)
有趣的checkSomething(severRequest:ServerRequest):Mono{
返回ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(
fromIterable(testList),String::class.java
).then(ServerResponse.temporaryRedirect(URI(“/test\u thymeleaf.html”)).build()
}
}
处理程序现在转发到一个html页面!太好了
感谢您的帮助,因此这将把我转发到一个名为best的URI,该URI将启动另一个处理程序。但是我想转发到html页面?或者我错过了什么。你可以重定向到任何你想要的URL,而不是最好的。或者您可以从
then()
块返回HTML
@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())
}
}