/** * A single place to customize the response body of all Exception types. * <p>The default implementation sets the {@link WebUtils#ERROR_EXCEPTION_ATTRIBUTE} * request attribute and creates a {@link ResponseEntity} from the given * body, headers, and status. * @param ex the exception * @param body the body for the response * @param headers the headers for the response * @param status the response status * @param request the current request */ protected ResponseEntity<Object> handleExceptionInternal( Exception ex, @Nullable Object body, HttpHeaders headers, HttpStatus status, WebRequest request) { if (HttpStatus.INTERNAL_SERVER_ERROR.equals(status)) { request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, ex, WebRequest.SCOPE_REQUEST); } return new ResponseEntity<>(body, headers, status); }
@Override public boolean test(Method method) { ResponseStatus annot = AnnotatedElementUtils.findMergedAnnotation(method, ResponseStatus.class); return annot != null && annot.code().equals(this.code); } }
private ResponseEntity<Object> testException(Exception ex) { try { ResponseEntity<Object> responseEntity = this.exceptionHandlerSupport.handleException(ex, this.request); // SPR-9653 if (HttpStatus.INTERNAL_SERVER_ERROR.equals(responseEntity.getStatusCode())) { assertSame(ex, this.servletRequest.getAttribute("javax.servlet.error.exception")); } this.defaultExceptionResolver.resolveException(this.servletRequest, this.servletResponse, null, ex); assertEquals(this.servletResponse.getStatus(), responseEntity.getStatusCode().value()); return responseEntity; } catch (Exception ex2) { throw new IllegalStateException("handleException threw exception", ex2); } }
if (getStatusCode().equals(HttpStatus.NO_CONTENT) && viewName == null) { return null;
@Override public boolean checkNotModified(@Nullable String etag, Instant lastModified) { HttpStatus status = getResponse().getStatusCode(); if (this.notModified || (status != null && !HttpStatus.OK.equals(status))) { return this.notModified;
@Test public void temporaryRedirect() { URI location = URI.create("http://example.com"); Mono<ServerResponse> result = ServerResponse.temporaryRedirect(location).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.TEMPORARY_REDIRECT.equals(response.statusCode()) && location.equals(response.headers().getLocation())) .expectComplete() .verify(); }
@Test public void permanentRedirect() { URI location = URI.create("http://example.com"); Mono<ServerResponse> result = ServerResponse.permanentRedirect(location).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.PERMANENT_REDIRECT.equals(response.statusCode()) && location.equals(response.headers().getLocation())) .expectComplete() .verify(); }
@Test public void created() { URI location = URI.create("http://example.com"); Mono<ServerResponse> result = ServerResponse.created(location).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.CREATED.equals(response.statusCode()) && location.equals(response.headers().getLocation())) .expectComplete() .verify(); }
@Test public void seeOther() { URI location = URI.create("http://example.com"); Mono<ServerResponse> result = ServerResponse.seeOther(location).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.SEE_OTHER.equals(response.statusCode()) && location.equals(response.headers().getLocation())) .expectComplete() .verify(); }
@Test public void from() { ServerResponse other = ServerResponse.ok().header("foo", "bar").build().block(); Mono<ServerResponse> result = ServerResponse.from(other).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.OK.equals(response.statusCode()) && "bar".equals(response.headers().getFirst("foo"))) .expectComplete() .verify(); }
@Test public void ok() { Mono<ServerResponse> result = ServerResponse.ok().build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.OK.equals(response.statusCode())) .expectComplete() .verify(); }
@Test public void noContent() { Mono<ServerResponse> result = ServerResponse.noContent().build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.NO_CONTENT.equals(response.statusCode())) .expectComplete() .verify(); }
@Test public void notFound() { Mono<ServerResponse> result = ServerResponse.notFound().build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.NOT_FOUND.equals(response.statusCode())) .expectComplete() .verify(); }
@Test public void unprocessableEntity() { Mono<ServerResponse> result = ServerResponse.unprocessableEntity().build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.UNPROCESSABLE_ENTITY.equals(response.statusCode())) .expectComplete() .verify(); }
@Test public void status() { Mono<ServerResponse> result = ServerResponse.status(HttpStatus.CREATED).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.CREATED.equals(response.statusCode())) .expectComplete() .verify(); }
@Test public void badRequest() { Mono<ServerResponse> result = ServerResponse.badRequest().build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.BAD_REQUEST.equals(response.statusCode())) .expectComplete() .verify(); }
@Test public void accepted() { Mono<ServerResponse> result = ServerResponse.accepted().build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.ACCEPTED.equals(response.statusCode())) .expectComplete() .verify(); }
@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(); }
@Test public void status() { String body = "foo"; Mono<EntityResponse<String>> result = EntityResponse.fromObject(body).status(HttpStatus.CREATED).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.CREATED.equals(response.statusCode())) .expectComplete() .verify(); }
private void testOnStatus(Throwable expected, Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) { HttpStatus errorStatus = HttpStatus.BAD_GATEWAY; this.server.enqueue(new MockResponse() .setResponseCode(errorStatus.value()) .setHeader("Content-Type", "application/json") .setChunkedBody("{\"error\" : {\"status\" : 502, \"message\" : \"Bad gateway.\"}}", 5)); Mono<String> mono = this.webClient.get() .uri("/json").accept(MediaType.APPLICATION_JSON) .retrieve() .onStatus(status -> status.equals(errorStatus), exceptionFunction) .bodyToMono(String.class); StepVerifier.create(mono).expectErrorSatisfies(actual -> assertSame(expected, actual)).verify(DELAY); assertEquals(1, this.server.getRequestCount()); }