diff --git a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java index f08bfc0788..b0c1e1086b 100644 --- a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java +++ b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java @@ -155,12 +155,14 @@ public class ResponseEntity extends HttpEntity { return builder.toString(); } + // Static builder methods /** * Creates a builder with the given status. * @param status the response status * @return the created builder + * @since 4.1 */ public static BodyBuilder status(HttpStatus status) { return new DefaultBuilder(status); @@ -170,6 +172,7 @@ public class ResponseEntity extends HttpEntity { * Creates a builder with the given status. * @param status the response status * @return the created builder + * @since 4.1 */ public static BodyBuilder status(int status) { return status(HttpStatus.valueOf(status)); @@ -178,6 +181,7 @@ public class ResponseEntity extends HttpEntity { /** * Creates 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); @@ -187,6 +191,7 @@ public class ResponseEntity extends HttpEntity { * A shortcut for creating a {@code ResponseEntity} with the given body and * status set to {@linkplain HttpStatus#OK OK}. * @return the created {@code ResponseEntity} + * @since 4.1 */ public static ResponseEntity ok(T body) { BodyBuilder builder = ok(); @@ -198,6 +203,7 @@ public class ResponseEntity extends HttpEntity { * 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); @@ -207,6 +213,7 @@ public class ResponseEntity extends HttpEntity { /** * Creates a builder with an {@link HttpStatus#ACCEPTED ACCEPTED} status. * @return the created builder + * @since 4.1 */ public static BodyBuilder accepted() { return status(HttpStatus.ACCEPTED); @@ -215,11 +222,21 @@ public class ResponseEntity extends HttpEntity { /** * Creates a builder with a {@link HttpStatus#NO_CONTENT NO_CONTENT} status. * @return the created builder + * @since 4.1 */ public static HeadersBuilder noContent() { return status(HttpStatus.NO_CONTENT); } + /** + * Creates a builder with a {@link HttpStatus#NOT_FOUND NOT_FOUND} status. + * @return the created builder + * @since 4.1 + */ + public static HeadersBuilder notFound() { + return status(HttpStatus.NOT_FOUND); + } + /** * Defines a builder that adds headers to the response entity. diff --git a/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java b/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java index d05d08728a..07b7828898 100644 --- a/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java +++ b/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java @@ -20,9 +20,13 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.List; -import static org.junit.Assert.*; import org.junit.Test; +import static org.junit.Assert.*; + +/** + * @author Arjen Poutsma + */ public class ResponseEntityTests { @Test @@ -97,6 +101,15 @@ public class ResponseEntityTests { assertNull(responseEntity.getBody()); } + @Test + public void notFound() throws URISyntaxException { + ResponseEntity responseEntity = ResponseEntity.notFound().build(); + + assertNotNull(responseEntity); + assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode()); + assertNull(responseEntity.getBody()); + } + @Test public void headers() throws URISyntaxException { String eTag = "\"foo\"";