How to redirect a request in spring webflux?

后端 未结 3 863
栀梦
栀梦 2021-01-19 02:20

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:

相关标签:
3条回答
  • 2021-01-19 02:47
    @Bean
    RouterFunction<ServerResponse> routerFunction() {
         route(GET("/redirect"), { req ->
              ServerResponse.temporaryRedirect(URI.create(TargetUrl))
                        .build()
            }
        })
    
    }
    

    Thanks you very much Johan Magnusson

    0 讨论(0)
  • 2021-01-19 02:49

    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());
        }
    }
    
    0 讨论(0)
  • 2021-01-19 02:49

    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")
    
    0 讨论(0)
提交回复
热议问题