/** * Create a builder with an {@linkplain HttpStatus#ACCEPTED ACCEPTED} status. * @return the created builder * @since 4.1 */ public static BodyBuilder accepted() { return status(HttpStatus.ACCEPTED); }
/** * Create a builder with an * {@linkplain HttpStatus#UNPROCESSABLE_ENTITY UNPROCESSABLE_ENTITY} status. * @return the created builder * @since 4.1.3 */ public static BodyBuilder unprocessableEntity() { return status(HttpStatus.UNPROCESSABLE_ENTITY); }
/** * Create a builder with a {@linkplain HttpStatus#NO_CONTENT NO_CONTENT} status. * @return the created builder * @since 4.1 */ public static HeadersBuilder<?> noContent() { return status(HttpStatus.NO_CONTENT); }
/** * Create a builder with a {@linkplain HttpStatus#NOT_FOUND NOT_FOUND} status. * @return the created builder * @since 4.1 */ public static HeadersBuilder<?> notFound() { return status(HttpStatus.NOT_FOUND); }
/** * Create a builder with the status set to {@linkplain HttpStatus#OK OK}. * @return the created builder * @since 4.1 */ public static BodyBuilder ok() { return status(HttpStatus.OK); }
/** * Create a builder with a {@linkplain HttpStatus#BAD_REQUEST BAD_REQUEST} status. * @return the created builder * @since 4.1 */ public static BodyBuilder badRequest() { return status(HttpStatus.BAD_REQUEST); }
/** * Create a new builder with a {@linkplain HttpStatus#CREATED CREATED} status * and a location header set to the given URI. * @param location the location URI * @return the created builder * @since 4.1 */ public static BodyBuilder created(URI location) { BodyBuilder builder = status(HttpStatus.CREATED); return builder.location(location); }
private <T> ResponseEntity<T> createEntity(T body, HttpHeaders headers, int status) { HttpStatus resolvedStatus = HttpStatus.resolve(status); return resolvedStatus != null ? new ResponseEntity<>(body, headers, resolvedStatus) : ResponseEntity.status(status).headers(headers).body(body); }
@ExceptionHandler public ResponseEntity<String> handle(IllegalStateException ex) { return ResponseEntity.status(400).body("Handled exception"); } }
@ExceptionHandler public ResponseEntity<Map<String, String>> handle(Spr16318Exception ex) { return ResponseEntity.status(500).body(Collections.singletonMap("reason", "error")); } }
@ExceptionHandler public ResponseEntity<Map<String, String>> handle(IllegalArgumentException ex) { return ResponseEntity.status(500).body(Collections.singletonMap("reason", "error")); } }
private <T> ResponseEntity<T> createEntity(HttpHeaders headers, int status) { HttpStatus resolvedStatus = HttpStatus.resolve(status); return resolvedStatus != null ? new ResponseEntity<>(headers, resolvedStatus) : ResponseEntity.status(status).headers(headers).build(); }
@Override public ResponseEntity<T> extractData(ClientHttpResponse response) throws IOException { if (this.delegate != null) { T body = this.delegate.extractData(response); return ResponseEntity.status(response.getRawStatusCode()).headers(response.getHeaders()).body(body); } else { return ResponseEntity.status(response.getRawStatusCode()).headers(response.getHeaders()).build(); } } }
@Test public void statusCodeAsInt() { Integer entity = 42; ResponseEntity<Integer> responseEntity = ResponseEntity.status(200).body(entity); assertEquals(200, responseEntity.getStatusCode().value()); assertEquals(entity, responseEntity.getBody()); }
@PostMapping("/messages-response-entity") public ResponseEntity<Void> sendMessage(RedirectAttributes attributes) { attributes.addFlashAttribute("successMessage", "yay!"); URI location = URI.create("/messages/1?name=value"); return ResponseEntity.status(HttpStatus.FOUND).location(location).build(); } }
@Test public void customStatusCode() { Integer entity = 42; ResponseEntity<Integer> responseEntity = ResponseEntity.status(299).body(entity); assertEquals(299, responseEntity.getStatusCodeValue()); assertEquals(entity, responseEntity.getBody()); }
@Test public void shouldHandleNotModifiedResponse() throws Exception { long currentTime = new Date().getTime(); long oneMinuteAgo = currentTime - (1000 * 60); String etagValue = "\"deadb33f8badf00d\""; ResponseEntity<String> returnValue = ResponseEntity.status(HttpStatus.NOT_MODIFIED) .eTag(etagValue).lastModified(oneMinuteAgo).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, oneMinuteAgo); }
@Test public void emptyCacheControl() { Integer entity = 42; ResponseEntity<Integer> responseEntity = ResponseEntity.status(HttpStatus.OK) .cacheControl(CacheControl.empty()) .body(entity); assertNotNull(responseEntity); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertFalse(responseEntity.getHeaders().containsKey(HttpHeaders.CACHE_CONTROL)); assertEquals(entity, responseEntity.getBody()); }
@Test public void cacheControlNoCache() { Integer entity = 42; ResponseEntity<Integer> responseEntity = ResponseEntity.status(HttpStatus.OK) .cacheControl(CacheControl.noStore()) .body(entity); assertNotNull(responseEntity); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertTrue(responseEntity.getHeaders().containsKey(HttpHeaders.CACHE_CONTROL)); assertEquals(entity, responseEntity.getBody()); String cacheControlHeader = responseEntity.getHeaders().getCacheControl(); assertThat(cacheControlHeader, Matchers.equalTo("no-store")); }
@Test //SPR-16921 public void disableRangeSupportIfContentRangePresent() throws Exception { ResponseEntity<Resource> returnValue = ResponseEntity .status(HttpStatus.PARTIAL_CONTENT) .header(HttpHeaders.RANGE, "bytes=0-5") .body(new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8))); given(resourceRegionMessageConverter.canWrite(any(), eq(null))).willReturn(true); given(resourceRegionMessageConverter.canWrite(any(), eq(APPLICATION_OCTET_STREAM))).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResponseEntityResource, mavContainer, webRequest); then(resourceRegionMessageConverter).should(never()).write(anyCollection(), any(), any()); assertEquals(206, servletResponse.getStatus()); }