Created
June 8, 2017 05:50
-
-
Save linux-china/302e541b106b3ba1e3952d4016906b99 to your computer and use it in GitHub Desktop.
Functional Web Route in Spring 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.boot.web.servlet.ServletRegistrationBean; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.http.server.reactive.HttpHandler; | |
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; | |
import org.springframework.web.reactive.config.EnableWebFlux; | |
import org.springframework.web.reactive.function.server.RouterFunction; | |
import org.springframework.web.server.adapter.WebHttpHandlerBuilder; | |
import reactor.core.publisher.Mono; | |
import static org.springframework.web.reactive.function.server.RequestPredicates.GET; | |
import static org.springframework.web.reactive.function.server.RouterFunctions.route; | |
import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; | |
import static org.springframework.web.reactive.function.server.ServerResponse.ok; | |
@Configuration | |
@EnableWebFlux | |
public class Routes { | |
private static String ROUTE_PREFIX = "/flux"; | |
public RouterFunction<?> routesBean() { | |
return | |
route( | |
GET(ROUTE_PREFIX + "/hi"), | |
request -> ok().body(Mono.just("hi"), String.class)) | |
; | |
} | |
@Bean | |
public ServletRegistrationBean routeServlet() throws Exception { | |
HttpHandler httpHandler = WebHttpHandlerBuilder | |
.webHandler(toHttpHandler(routesBean())) | |
.build(); | |
ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(new ServletHttpHandlerAdapter(httpHandler), ROUTE_PREFIX + "/*"); | |
registrationBean.setLoadOnStartup(1); | |
registrationBean.setAsyncSupported(true); | |
return registrationBean; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment