@Override public HttpMethod method() { return this.delegate.method(); }
@Override public HttpMethod method() { return this.request.method(); }
@Test public void method() { HttpMethod method = HttpMethod.POST; when(mockRequest.method()).thenReturn(method); assertSame(method, wrapper.method()); }
@Override public Mono<ServerResponse> handle(ServerRequest request) { HttpMethod method = request.method(); if (method != null) { switch (method) { case GET: return EntityResponse.fromObject(this.resource).build() .map(response -> response); case HEAD: Resource headResource = new HeadMethodResource(this.resource); return EntityResponse.fromObject(headResource).build() .map(response -> response); case OPTIONS: return ServerResponse.ok() .allow(SUPPORTED_METHODS) .body(BodyInserters.empty()); } } return ServerResponse.status(HttpStatus.METHOD_NOT_ALLOWED) .allow(SUPPORTED_METHODS) .body(BodyInserters.empty()); }
@Test public void from() { MockServerHttpRequest request = MockServerHttpRequest.post("http://example.com") .header("foo", "bar") .build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); ServerRequest other = ServerRequest.create(exchange, HandlerStrategies.withDefaults().messageReaders()); Flux<DataBuffer> body = Flux.just("baz") .map(s -> s.getBytes(StandardCharsets.UTF_8)) .map(dataBufferFactory::wrap); ServerRequest result = ServerRequest.from(other) .method(HttpMethod.HEAD) .headers(httpHeaders -> httpHeaders.set("foo", "baar")) .cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build())) .body(body) .build(); assertEquals(HttpMethod.HEAD, result.method()); assertEquals(1, result.headers().asHttpHeaders().size()); assertEquals("baar", result.headers().asHttpHeaders().getFirst("foo")); assertEquals(1, result.cookies().size()); assertEquals("quux", result.cookies().getFirst("baz").getValue()); StepVerifier.create(result.bodyToFlux(String.class)) .expectNext("baz") .verifyComplete(); }
@Override public boolean test(ServerRequest request) { return this.httpMethod == request.method(); } }
@Override public HttpMethod method() { return this.request.method(); }
@Override public HttpMethod method() { return this.request.method(); }
/** * @param request The HTTP request. * @return A "method" tag whose value is a capitalized method (e.g. GET). */ public static Tag method(ServerRequest request) { return Tag.of("method", request.method().toString()); }
@Override public Mono<ServerResponse> handle(ServerRequest request) { switch (request.method()) { case GET: return ServerResponse.ok() .body(BodyInserters.fromResource(this.resource)); case HEAD: Resource headResource = new HeadMethodResource(this.resource); return ServerResponse.ok() .body(BodyInserters.fromResource(headResource)); case OPTIONS: return ServerResponse.ok() .allow(SUPPORTED_METHODS) .body(BodyInserters.empty()); default: return ServerResponse.status(HttpStatus.METHOD_NOT_ALLOWED) .allow(SUPPORTED_METHODS) .body(BodyInserters.empty()); } }
/** * 注册自定义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; }