|
| 1 | +package com.baeldung.functional; |
| 2 | + |
| 3 | +import static org.springframework.web.reactive.function.BodyInserters.fromObject; |
| 4 | +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; |
| 5 | +import static org.springframework.web.reactive.function.server.RouterFunctions.route; |
| 6 | +import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; |
| 7 | +import static org.springframework.web.reactive.function.server.ServerResponse.ok; |
| 8 | + |
| 9 | +import org.apache.catalina.Context; |
| 10 | +import org.apache.catalina.startup.Tomcat; |
| 11 | +import org.springframework.boot.web.embedded.tomcat.TomcatWebServer; |
| 12 | +import org.springframework.boot.web.server.WebServer; |
| 13 | +import org.springframework.core.io.ClassPathResource; |
| 14 | +import org.springframework.http.server.reactive.HttpHandler; |
| 15 | +import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; |
| 16 | +import org.springframework.web.reactive.function.server.RouterFunction; |
| 17 | +import org.springframework.web.reactive.function.server.RouterFunctions; |
| 18 | +import org.springframework.web.reactive.function.server.ServerResponse; |
| 19 | +import org.springframework.web.server.WebHandler; |
| 20 | +import org.springframework.web.server.adapter.WebHttpHandlerBuilder; |
| 21 | + |
| 22 | +public class ExploreSpring5URLPatternUsingRouterFunctions { |
| 23 | + |
| 24 | + private RouterFunction<ServerResponse> routingFunction() { |
| 25 | + |
| 26 | + return route(GET("/p?ths"), serverRequest -> ok().body(fromObject("/p?ths"))).andRoute(GET("/test/{*id}"), serverRequest -> ok().body(fromObject(serverRequest.pathVariable("id")))) |
| 27 | + .andRoute(GET("/*card"), serverRequest -> ok().body(fromObject("/*card path was accessed"))) |
| 28 | + .andRoute(GET("/{var1}_{var2}"), serverRequest -> ok().body(fromObject(serverRequest.pathVariable("var1") + " , " + serverRequest.pathVariable("var2")))) |
| 29 | + .andRoute(GET("/{baeldung:[a-z]+}"), serverRequest -> ok().body(fromObject("/{baeldung:[a-z]+} was accessed and baeldung=" + serverRequest.pathVariable("baeldung")))) |
| 30 | + .and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/"))); |
| 31 | + } |
| 32 | + |
| 33 | + WebServer start() throws Exception { |
| 34 | + WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction()); |
| 35 | + HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler) |
| 36 | + .prependFilter(new IndexRewriteFilter()) |
| 37 | + .build(); |
| 38 | + |
| 39 | + Tomcat tomcat = new Tomcat(); |
| 40 | + tomcat.setHostname("localhost"); |
| 41 | + tomcat.setPort(9090); |
| 42 | + Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir")); |
| 43 | + ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); |
| 44 | + Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet); |
| 45 | + rootContext.addServletMappingDecoded("/", "httpHandlerServlet"); |
| 46 | + |
| 47 | + TomcatWebServer server = new TomcatWebServer(tomcat); |
| 48 | + server.start(); |
| 49 | + return server; |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + public static void main(String[] args) { |
| 54 | + try { |
| 55 | + new FunctionalWebApplication().start(); |
| 56 | + } catch (Exception e) { |
| 57 | + e.printStackTrace(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments