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 360f0321c3..2d60ec4734 100644 --- a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java +++ b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java @@ -79,7 +79,7 @@ import org.springframework.util.ObjectUtils; */ public class ResponseEntity extends HttpEntity { - private final Object status; + private final HttpStatusCode status; /** @@ -108,16 +108,6 @@ public class ResponseEntity extends HttpEntity { this(null, headers, status); } - /** - * Create a {@code ResponseEntity} with a body, headers, and a status code. - * @param body the entity body - * @param headers the entity headers - * @param status the status code - */ - public ResponseEntity(@Nullable T body, @Nullable MultiValueMap headers, HttpStatusCode status) { - this(body, headers, (Object) status); - } - /** * Create a {@code ResponseEntity} with a body, headers, and a raw status code. * @param body the entity body @@ -126,16 +116,20 @@ public class ResponseEntity extends HttpEntity { * @since 5.3.2 */ public ResponseEntity(@Nullable T body, @Nullable MultiValueMap headers, int rawStatus) { - this(body, headers, (Object) rawStatus); + this(body, headers, HttpStatusCode.valueOf(rawStatus)); } /** - * Private constructor. + * Create a {@code ResponseEntity} with a body, headers, and a status code. + * @param body the entity body + * @param headers the entity headers + * @param statusCode the status code */ - private ResponseEntity(@Nullable T body, @Nullable MultiValueMap headers, Object status) { + public ResponseEntity(@Nullable T body, @Nullable MultiValueMap headers, HttpStatusCode statusCode) { super(body, headers); - Assert.notNull(status, "HttpStatusCode must not be null"); - this.status = status; + Assert.notNull(statusCode, "HttpStatusCode must not be null"); + + this.status = statusCode; } @@ -144,12 +138,7 @@ public class ResponseEntity extends HttpEntity { * @return the HTTP status as an HttpStatus enum entry */ public HttpStatusCode getStatusCode() { - if (this.status instanceof HttpStatusCode statusCode) { - return statusCode; - } - else { - return HttpStatusCode.valueOf((Integer) this.status); - } + return this.status; } /** @@ -161,12 +150,7 @@ public class ResponseEntity extends HttpEntity { */ @Deprecated(since = "6.0") public int getStatusCodeValue() { - if (this.status instanceof HttpStatusCode statusCode) { - return statusCode.value(); - } - else { - return (Integer) this.status; - } + return getStatusCode().value(); } @@ -530,14 +514,20 @@ public class ResponseEntity extends HttpEntity { private static class DefaultBuilder implements BodyBuilder { - private final Object statusCode; + private final HttpStatusCode statusCode; private final HttpHeaders headers = new HttpHeaders(); - public DefaultBuilder(Object statusCode) { + + public DefaultBuilder(int statusCode) { + this(HttpStatusCode.valueOf(statusCode)); + } + + public DefaultBuilder(HttpStatusCode statusCode) { this.statusCode = statusCode; } + @Override public BodyBuilder header(String headerName, String... headerValues) { for (String headerValue : headerValues) {