/** * Create a builder with an * {@linkplain HttpStatus#UNPROCESSABLE_ENTITY 422 Unprocessable Entity} status. * @return the created builder */ static BodyBuilder unprocessableEntity() { return status(HttpStatus.UNPROCESSABLE_ENTITY); }
@Test public void status() { Mono<ServerResponse> result = ServerResponse.status(HttpStatus.CREATED).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.CREATED.equals(response.statusCode())) .expectComplete() .verify(); }
/** * Create a builder with the status set to {@linkplain HttpStatus#OK 200 OK}. * @return the created builder */ static BodyBuilder ok() { return status(HttpStatus.OK); }
@Test public void statusCode() { HttpStatus statusCode = HttpStatus.ACCEPTED; Mono<ServerResponse> result = ServerResponse.status(statusCode).build(); StepVerifier.create(result) .expectNextMatches(response -> statusCode.equals(response.statusCode())) .expectComplete() .verify(); }
/** * Create a builder with an {@linkplain HttpStatus#ACCEPTED 202 Accepted} status. * @return the created builder */ static BodyBuilder accepted() { return status(HttpStatus.ACCEPTED); }
/** * Create a builder with a {@linkplain HttpStatus#NO_CONTENT 204 No Content} status. * @return the created builder */ static HeadersBuilder<?> noContent() { return status(HttpStatus.NO_CONTENT); }
/** * Create a builder with a {@linkplain HttpStatus#BAD_REQUEST 400 Bad Request} status. * @return the created builder */ static BodyBuilder badRequest() { return status(HttpStatus.BAD_REQUEST); }
/** * Create a builder with a {@linkplain HttpStatus#NOT_FOUND 404 Not Found} status. * @return the created builder */ static HeadersBuilder<?> notFound() { return status(HttpStatus.NOT_FOUND); }
/** * Create a builder with a {@linkplain HttpStatus#PERMANENT_REDIRECT 308 Permanent Redirect} * status and a location header set to the given URI. * @param location the location URI * @return the created builder */ static BodyBuilder permanentRedirect(URI location) { BodyBuilder builder = status(HttpStatus.PERMANENT_REDIRECT); return builder.location(location); }
/** * Create a new builder with a {@linkplain HttpStatus#CREATED 201 Created} status * and a location header set to the given URI. * @param location the location URI * @return the created builder */ static BodyBuilder created(URI location) { BodyBuilder builder = status(HttpStatus.CREATED); return builder.location(location); }
/** * Create a builder with a {@linkplain HttpStatus#TEMPORARY_REDIRECT 307 Temporary Redirect} * status and a location header set to the given URI. * @param location the location URI * @return the created builder */ static BodyBuilder temporaryRedirect(URI location) { BodyBuilder builder = status(HttpStatus.TEMPORARY_REDIRECT); return builder.location(location); }
/** * Create a builder with a {@linkplain HttpStatus#SEE_OTHER 303 See Other} * status and a location header set to the given URI. * @param location the location URI * @return the created builder */ static BodyBuilder seeOther(URI location) { BodyBuilder builder = status(HttpStatus.SEE_OTHER); return builder.location(location); }
@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()); }
return responseMono; }) .onError(IllegalStateException.class, (e, request) -> ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build()) .build();
@Test public void build() { ResponseCookie cookie = ResponseCookie.from("name", "value").build(); Mono<ServerResponse> result = ServerResponse.status(HttpStatus.CREATED) .header("MyKey", "MyValue") .cookie(cookie).build(); MockServerHttpRequest request = MockServerHttpRequest.get("http://example.com").build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); result.flatMap(res -> res.writeTo(exchange, EMPTY_CONTEXT)).block(); MockServerHttpResponse response = exchange.getResponse(); assertEquals(HttpStatus.CREATED, response.getStatusCode()); assertEquals("MyValue", response.getHeaders().getFirst("MyKey")); assertEquals("value", response.getCookies().getFirst("name").getValue()); StepVerifier.create(response.getBody()).expectComplete().verify(); }
/** * Create a builder with a {@linkplain HttpStatus#BAD_REQUEST BAD_REQUEST} status. * * @return the created builder */ static BodyBuilder badRequest() { return status(HttpStatus.BAD_REQUEST); }
/** * Create a builder with a {@linkplain HttpStatus#NOT_FOUND NOT_FOUND} status. * * @return the created builder */ static HeadersBuilder<?> notFound() { return status(HttpStatus.NOT_FOUND); }
@Before public void setUp() { RouterFunction<ServerResponse> route = RouterFunctions .route(RequestPredicates.GET("/"), (request) -> ServerResponse.status(HttpStatus.OK) .body(fromObject(new Person("Jane", "Doe")))) .andRoute(RequestPredicates.GET("/{foo}/{bar}"), (request) -> ServerResponse.status(HttpStatus.OK) .body(fromObject(new Person("Jane", "Doe")))) .andRoute(RequestPredicates.POST("/upload"), (request) -> request.body(BodyExtractors.toMultipartData()) .map((parts) -> ServerResponse.status(HttpStatus.OK) .build().block())) .andRoute(RequestPredicates.GET("/set-cookie"), (request) -> ServerResponse.ok() .cookie(ResponseCookie.from("name", "value") .domain("localhost").httpOnly(true).build()) .build()); this.webTestClient = WebTestClient.bindToRouterFunction(route).configureClient() .baseUrl("https://api.example.com") .filter(documentationConfiguration(this.restDocumentation)).build(); }
<T extends Throwable> Mono<ServerResponse> getResponse(final Mono<T> monoError) { return monoError.transform(ThrowableTranslator::translate) .flatMap(translation -> ServerResponse .status(translation.getHttpStatus()) .body(Mono.just(new ErrorResponse(translation.getMessage())), ErrorResponse.class)); } }
@SuppressWarnings({ "unchecked" }) public <T> RouterFunction<?> functionEndpoints() { return route(POST("/"), request -> { Class<T> outputType = (Class<T>) this.inspector.getOutputType(this.function); FunctionWrapper wrapper = RequestProcessor.wrapper(function, null, null); Mono<ResponseEntity<?>> stream = request.bodyToMono(String.class) .flatMap(content -> processor.post(wrapper, content, false)); return stream.flatMap(entity -> status(entity.getStatusCode()) .headers(headers -> headers.addAll(entity.getHeaders())) .body(Mono.just((T) entity.getBody()), outputType)); }); }