Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect a request in spring webflux?

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() )
        })
like image 581
Mahdi Amini Avatar asked May 24 '18 06:05

Mahdi Amini


2 Answers

@Bean
RouterFunction<ServerResponse> routerFunction() {
     route(GET("/redirect"), { req ->
          ServerResponse.temporaryRedirect(URI.create(TargetUrl))
                    .build()
        }
    })

}

Thanks you very much Johan Magnusson

like image 158
Mahdi Amini Avatar answered Nov 15 '22 07:11

Mahdi Amini


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());
    }
}
like image 34
JavaCodeNet Avatar answered Nov 15 '22 08:11

JavaCodeNet