/** * Return a {@code RequestPredicate} that matches if request's HTTP method is {@code GET} * 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 GET and if the given pattern * matches against the request path */ public static RequestPredicate GET(String pattern) { return method(HttpMethod.GET).and(path(pattern)); }
/** * Return a {@code RequestPredicate} that matches if request's HTTP method is {@code HEAD} * 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 HEAD and if the given pattern * matches against the request path */ public static RequestPredicate HEAD(String pattern) { return method(HttpMethod.HEAD).and(path(pattern)); }
/** * Return a {@code RequestPredicate} that matches if request's HTTP method is {@code OPTIONS} * 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 OPTIONS and if the given pattern * matches against the request path */ public static RequestPredicate OPTIONS(String pattern) { return method(HttpMethod.OPTIONS).and(path(pattern)); }
/** * 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 public RouterFunctions.Builder GET(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction) { return add(RequestPredicates.GET(pattern).and(predicate), handlerFunction); }
@Override public RouterFunctions.Builder POST(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction) { return add(RequestPredicates.POST(pattern).and(predicate), handlerFunction); }
@Override public RouterFunctions.Builder OPTIONS(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction) { return add(RequestPredicates.OPTIONS(pattern).and(predicate), handlerFunction); }
/** * Return a {@code RequestPredicate} that matches if request's HTTP method is {@code POST} * 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 POST and if the given pattern * matches against the request path */ public static RequestPredicate POST(String pattern) { return method(HttpMethod.POST).and(path(pattern)); }
/** * Return a {@code RequestPredicate} that matches if request's HTTP method is {@code PUT} * 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 PUT and if the given pattern * matches against the request path */ public static RequestPredicate PUT(String pattern) { return method(HttpMethod.PUT).and(path(pattern)); }
/** * Return a {@code RequestPredicate} that matches if request's HTTP method is {@code PATCH} * 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 PATCH and if the given pattern * matches against the request path */ public static RequestPredicate PATCH(String pattern) { return method(HttpMethod.PATCH).and(path(pattern)); }
@Override public RouterFunctions.Builder HEAD(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction) { return add(RequestPredicates.HEAD(pattern).and(predicate), handlerFunction); }
@Override public RouterFunctions.Builder PUT(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction) { return add(RequestPredicates.PUT(pattern).and(predicate), handlerFunction); }
@Override public RouterFunctions.Builder DELETE(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction) { return add(RequestPredicates.DELETE(pattern).and(predicate), handlerFunction); }
@Override public RouterFunctions.Builder PATCH(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction) { return add(RequestPredicates.PATCH(pattern).and(predicate), handlerFunction); }
@Test public void and() { RequestPredicate predicate1 = request -> true; RequestPredicate predicate2 = request -> true; RequestPredicate predicate3 = request -> false; MockServerRequest request = MockServerRequest.builder().build(); assertTrue(predicate1.and(predicate2).test(request)); assertTrue(predicate2.and(predicate1).test(request)); assertFalse(predicate1.and(predicate3).test(request)); }
@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()); }
@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 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); }
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)))); }
public RouterFunction<ServerResponse> routes() { return route(GET("/posts"), this.postHandler::all) .andRoute(POST("/posts").and(contentType(APPLICATION_JSON)), this.postHandler::create) .andRoute(GET("/posts/{id}"), this.postHandler::get); }