I'm looking for a clear documentation on how I can create a Router for Spring Webflux in kotlin that directs the user for both Rest and web Endpoints.
I made router to do the rest points with no issue. but couldn't do it for the web, always getting an error message of:
java.lang.IllegalArgumentException: Could not resolve view with name 'index'
at org.springframework.web.reactive.function.server.DefaultRenderingResponseBuilder$DefaultRenderingResponse.lambda$writeToInternal$1(DefaultRenderingResponseBuilder.java:197) ~[spring-webflux-6.0.7.jar:6.0.7]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
*__checkpoint ⇢ Handler com.springinaction.router.config.WebConfiguration$$Lambda$524/0x0000000800519368@46b9842f [DispatcherHandler]
*__checkpoint ⇢ HTTP GET "/index" [ExceptionHandlingWebHandler]
Original Stack Trace:
I tried to the router in several ways
@Bean
fun router(): RouterFunction<*> {
return resources("/**", ClassPathResource("/templates/"))
.andOther(
route(GET("/index"), HandlerFunction {
ServerResponse
.ok()
.render("index")
})
.andOther (
route(
GET("/hello"), HandlerFunction {
ServerResponse
.ok()
.contentType(MediaType.TEXT_HTML)
.bodyValue("Hello Router World")
}
)
)
)
}
and this way as well
@Bean
fun router(): RouterFunction<*> {
return router {
GET("/hello") {
ServerResponse
.ok()
.contentType(MediaType.TEXT_HTML)
.bodyValue("Hello Router World")
}
GET("/index") {
ServerResponse
.ok()
.render("index")
}
}
}
same error...
how can I made the router to resolve the index.html
file and load it from the /templates
package?
regards
The solution was pretty simple, just remove the ServerResponse
and it will work just fine.
@Bean
fun myRouter(): RouterFunction<*> {
return router {
GET("/router") {
ok().render("mypage.html")
}
}
}
either this way or nested way or else will work...
@Bean
fun myRouter(): RouterFunction<*> {
return router {
accept(MediaType.TEXT_HTML).nest {
GET("/router") {
ok().render("mypage.html")
}
}
}
}