RouterFunction<ServerResponse> route = RouterFunctions.route() .GET("/foo", request -> ServerResponse.ok().build()) .POST("/", RequestPredicates.contentType(MediaType.TEXT_PLAIN), request -> ServerResponse.noContent().build()) .build(); System.out.println(route);
@Test public void contentType() { MediaType json = MediaType.APPLICATION_JSON; RequestPredicate predicate = RequestPredicates.contentType(json); MockServerRequest request = MockServerRequest.builder().header("Content-Type", json.toString()).build(); assertTrue(predicate.test(request)); request = MockServerRequest.builder().build(); assertFalse(predicate.test(request)); }
@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))"); }
@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 postHandler) { return route(GET("/posts"), postHandler::all) .andRoute(POST("/posts").and(contentType(APPLICATION_JSON)), postHandler::create) .andRoute(GET("/posts/{id}"), postHandler::get); } }
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); }
@Bean public RouterFunction<ServerResponse> route(QuoteHandler quoteHandler) { return RouterFunctions .route(GET("/hello").and(accept(TEXT_PLAIN)), quoteHandler::hello) .andRoute(POST("/echo").and(accept(TEXT_PLAIN).and(contentType(TEXT_PLAIN))), quoteHandler::echo) .andRoute(GET("/quotes").and(accept(APPLICATION_JSON)), quoteHandler::fetchQuotes) .andRoute(GET("/quotes").and(accept(APPLICATION_STREAM_JSON)), quoteHandler::streamQuotes); } }
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)); }
/** * 注册自定义RouterFunction * RoouterFunction 类似 Spring Web MVC 的 @RequestMapping * 用来定义路由信息,每个路由会映射到一个处理方法,当接受 HTTP 请求时候会调用该处理方法。 */ @Bean public RouterFunction<ServerResponse> restaurantRouter() { RouterFunction<ServerResponse> router = route(GET("/reactive/restaurants").and(accept(APPLICATION_JSON_UTF8)), restaurantHandler::findAll) .andRoute(GET("/reactive/delay/restaurants").and(accept(APPLICATION_JSON_UTF8)), restaurantHandler::findAllDelay) .andRoute(GET("/reactive/restaurants/{id}").and(accept(APPLICATION_JSON_UTF8)), restaurantHandler::get) .andRoute(POST("/reactive/restaurants").and(accept(APPLICATION_JSON_UTF8)).and(contentType(APPLICATION_JSON_UTF8)), restaurantHandler::create) .andRoute(DELETE("/reactive/restaurants/{id}").and(accept(APPLICATION_JSON_UTF8)), restaurantHandler::delete) // 注册自定义HandlerFilterFunction .filter((request, next) -> { if (HttpMethod.PUT.equals(request.method())) { return ServerResponse.status(HttpStatus.BAD_REQUEST).build(); } return next.handle(request); }); return router; }