How to redirect a request in spring webflux?

后端 未结 3 862
栀梦
栀梦 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: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 routerFunction() {
            return  route(GET("/"), req ->
                    ServerResponse.temporaryRedirect(URI.create("/login"))
                            .build());
        }
    }
    

提交回复
热议问题