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> 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());
}
}
I managed to achieve this using the code snippet below. I passed a redirect string to the render function.
The code below redirects to login route.
ServerResponse.ok().render("redirect:/login")