how can I create a redirect rest web service in spring WebFlux? It seems that there is no redirect functionality in WebFlux yet!
I want something like this:
@Bean
RouterFunction<ServerResponse> monoRouterFunction() {
return
route(GET("/redirect/{id}"),{
req -> req.Redirect( fetchAUrlFromDataBase() )
})
@Bean
RouterFunction<ServerResponse> routerFunction() {
route(GET("/redirect"), { req ->
ServerResponse.temporaryRedirect(URI.create(TargetUrl))
.build()
}
})
}
Thanks you very much Johan Magnusson
You can add below code in Spring boot main class to redirect '/' request to '/login' page.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
RouterFunction<ServerResponse> routerFunction() {
return route(GET("/"), req ->
ServerResponse.temporaryRedirect(URI.create("/login"))
.build());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With