@Test public void predicates() { testPredicate(methods(HttpMethod.GET), "GET"); testPredicate(methods(HttpMethod.GET, HttpMethod.POST), "[GET, POST]"); testPredicate(path("/foo"), "/foo"); testPredicate(pathExtension("foo"), "*.foo"); testPredicate(contentType(MediaType.APPLICATION_JSON), "Content-Type: application/json"); testPredicate(contentType(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN), "Content-Type: [application/json, text/plain]"); testPredicate(accept(MediaType.APPLICATION_JSON), "Accept: application/json"); testPredicate(queryParam("foo", "bar"), "?foo == bar"); testPredicate(method(HttpMethod.GET).and(path("/foo")), "(GET && /foo)"); testPredicate(method(HttpMethod.GET).or(path("/foo")), "(GET || /foo)"); testPredicate(method(HttpMethod.GET).negate(), "!(GET)"); testPredicate(GET("/foo") .or(contentType(MediaType.TEXT_PLAIN)) .and(accept(MediaType.APPLICATION_JSON).negate()), "(((GET && /foo) || Content-Type: text/plain) && !(Accept: application/json))"); }
@Override public RouterFunctions.Builder DELETE(String pattern, HandlerFunction<ServerResponse> handlerFunction) { return add(RequestPredicates.DELETE(pattern), handlerFunction); }
/** * Return a {@code RequestPredicate} that matches if request's HTTP method is {@code DELETE} * and the given {@code pattern} matches against the request path. * @param pattern the path pattern to match against * @return a predicate that matches if the request method is DELETE and if the given pattern * matches against the request path */ public static RequestPredicate DELETE(String pattern) { return method(HttpMethod.DELETE).and(path(pattern)); }
@Override protected RouterFunction<?> routerFunction() { NestedHandler nestedHandler = new NestedHandler(); return nest(path("/foo/"), route(GET("/bar"), nestedHandler::pattern) .andRoute(GET("/baz"), nestedHandler::pattern)) .andNest(GET("/{foo}"), route(GET("/bar"), nestedHandler::variables).and( nest(GET("/{bar}"), route(GET("/{baz}"), nestedHandler::variables)))) .andRoute(path("/{qux}/quux").and(method(HttpMethod.GET)), nestedHandler::variables) .andRoute(all(), nestedHandler::variables); }
@Override public RouterFunctions.Builder GET(String pattern, HandlerFunction<ServerResponse> handlerFunction) { return add(RequestPredicates.GET(pattern), handlerFunction); }
@Bean public RouterFunction<ServerResponse> route(TestHandler testHandler) { return RouterFunctions .route(RequestPredicates.GET("/route") .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), testHandler::route) .andRoute(RequestPredicates.POST("/route2") .and(RequestPredicates.contentType(MediaType.APPLICATION_JSON)), testHandler::route2) .andRoute(RequestPredicates.HEAD("/route3"), request -> ServerResponse.ok().build()); }
@Bean public RouterFunction<ServerResponse> routes(PostHandler postController) { return route(GET("/posts"), postController::all) .andRoute(POST("/posts"), postController::create) .andRoute(GET("/posts/{id}"), postController::get) .andRoute(PUT("/posts/{id}"), postController::update) .andRoute(DELETE("/posts/{id}"), postController::delete); }
public RouterFunction<ServerResponse> routes() { return nest(path("/test"), route(POST("/helloWorld2").and(accept(APPLICATION_JSON)), this::postHandler) .and(route(POST("/helloWorld"), this::getHandler) .and(route(GET("/rewrite"), this::rewriteHandler)) .and(route(GET("/pdm"), this::pdmHttpGet)) .and(route(GET("/oms"), this::omsHttpGet)) .and(route(GET("/timeout"), this::testRetry)))); }
@Bean public RouterFunction<ServerResponse> routes(PostHandler postHandler) { return route(GET("/posts"), postHandler::all) .andRoute(POST("/posts").and(contentType(APPLICATION_JSON)), postHandler::create) .andRoute(GET("/posts/{id}"), postHandler::get); } }
@Bean public RouterFunction<ServerResponse> routerFunctions() { return nest(path("/api/users"), nest(accept(APPLICATION_JSON), route(GET("/{id}"), userHandler::getUserById) .andRoute(method(HttpMethod.GET), userHandler::getAllUsers) .andRoute(DELETE("/{id}"), userHandler::deleteUser) .andRoute(POST("/"), userHandler::saveUser))); }
public RouterFunction<ServerResponse> routingFunction() { PersonRepository repository = new DummyPersonRepository(); PersonHandler handler = new PersonHandler(repository); return nest(path("/person"), nest(accept(APPLICATION_JSON), route(GET("/{id}"), handler::getPerson) .andRoute(method(HttpMethod.GET), handler::listPeople) ).andRoute(POST("/").and(contentType(APPLICATION_JSON)), handler::createPerson)); }
@Override public RouterFunctions.Builder POST(String pattern, HandlerFunction<ServerResponse> handlerFunction) { return add(RequestPredicates.POST(pattern), handlerFunction); }
@Test public void allMethods() { URI uri = URI.create("http://localhost/path"); RequestPredicate predicate = RequestPredicates.GET("/p*"); MockServerRequest request = MockServerRequest.builder().method(HttpMethod.GET).uri(uri).build(); assertTrue(predicate.test(request)); predicate = RequestPredicates.HEAD("/p*"); request = MockServerRequest.builder().method(HttpMethod.HEAD).uri(uri).build(); assertTrue(predicate.test(request)); predicate = RequestPredicates.POST("/p*"); request = MockServerRequest.builder().method(HttpMethod.POST).uri(uri).build(); assertTrue(predicate.test(request)); predicate = RequestPredicates.PUT("/p*"); request = MockServerRequest.builder().method(HttpMethod.PUT).uri(uri).build(); assertTrue(predicate.test(request)); predicate = RequestPredicates.PATCH("/p*"); request = MockServerRequest.builder().method(HttpMethod.PATCH).uri(uri).build(); assertTrue(predicate.test(request)); predicate = RequestPredicates.DELETE("/p*"); request = MockServerRequest.builder().method(HttpMethod.DELETE).uri(uri).build(); assertTrue(predicate.test(request)); predicate = RequestPredicates.OPTIONS("/p*"); request = MockServerRequest.builder().method(HttpMethod.OPTIONS).uri(uri).build(); assertTrue(predicate.test(request)); }
@Bean protected RouterFunction<ServerResponse> routingFunction(GraphAlgorithmHandler graphAlgorithmHandler) { return route(RequestPredicates.POST("/import"), graphAlgorithmHandler::importGraph) .andRoute(RequestPredicates.POST("/prepare"), graphAlgorithmHandler::prepareGraph) .andRoute(RequestPredicates.POST("/pregel"), graphAlgorithmHandler::configure) .andRoute(RequestPredicates.POST("/pregel/{id}"), graphAlgorithmHandler::run) .andRoute(RequestPredicates.GET("/pregel/{id}"), graphAlgorithmHandler::state) .andRoute(RequestPredicates.GET("/pregel/{id}/result"), graphAlgorithmHandler::result) .andRoute(RequestPredicates.DELETE("/pregel/{id}"), graphAlgorithmHandler::delete); } }
@Bean public RouterFunction<?> routerFunction() { return route(GET("/api/user").and(accept(MediaType.APPLICATION_JSON)), userHandler::handleGetUsers) .and(route(GET("/api/user/{id}").and(accept(MediaType.APPLICATION_JSON)), userHandler::handleGetUserById)); // .and(route(POST("/users"), userHandler::handleGetUsers)); }
@Bean RouterFunction routes(final HandlerFunction<ServerResponse> fallbackHandler) { return nest( path("/"), nest( accept(APPLICATION_JSON), route( GET("/api/orders"), getOrdersHandler() ) ).andNest( accept(APPLICATION_JSON), route( GET("/api"), getCountQueryHandler() ) ) ).andOther( route( GET("/**"), fallbackHandler ) ) ; } }
@Override public RouterFunctions.Builder path(String pattern, Consumer<RouterFunctions.Builder> builderConsumer) { return nest(RequestPredicates.path(pattern), builderConsumer); }