/** * 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)); }
@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))"); }
/** * 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)); }
/** * 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 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)); }
@Test public void method() { HttpMethod httpMethod = HttpMethod.GET; RequestPredicate predicate = RequestPredicates.method(httpMethod); MockServerRequest request = MockServerRequest.builder().method(httpMethod).build(); assertTrue(predicate.test(request)); request = MockServerRequest.builder().method(HttpMethod.POST).build(); assertFalse(predicate.test(request)); }
@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); }
/** * 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 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 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 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)); }
/** * 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)); }
/** * 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 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)); }
@Bean public RouterFunction<ServerResponse> routerFunction() { return nest(path("/test/{something}"), route(GET("/info"), this::variables) .andRoute(GET("/info/{foo}"), this::variables)) .andRoute(method(HttpMethod.GET), this::variables); }
@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)); }