Skip to content

Commit 5547361

Browse files
committed
Polishing
Store status code as HttpStatusCode instead of Object.
1 parent 805d643 commit 5547361

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

spring-web/src/main/java/org/springframework/http/ResponseEntity.java

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
*/
8080
public class ResponseEntity<T> extends HttpEntity<T> {
8181

82-
private final Object status;
82+
private final HttpStatusCode status;
8383

8484

8585
/**
@@ -108,16 +108,6 @@ public ResponseEntity(MultiValueMap<String, String> headers, HttpStatusCode stat
108108
this(null, headers, status);
109109
}
110110

111-
/**
112-
* Create a {@code ResponseEntity} with a body, headers, and a status code.
113-
* @param body the entity body
114-
* @param headers the entity headers
115-
* @param status the status code
116-
*/
117-
public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatusCode status) {
118-
this(body, headers, (Object) status);
119-
}
120-
121111
/**
122112
* Create a {@code ResponseEntity} with a body, headers, and a raw status code.
123113
* @param body the entity body
@@ -126,16 +116,20 @@ public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String>
126116
* @since 5.3.2
127117
*/
128118
public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, int rawStatus) {
129-
this(body, headers, (Object) rawStatus);
119+
this(body, headers, HttpStatusCode.valueOf(rawStatus));
130120
}
131121

132122
/**
133-
* Private constructor.
123+
* Create a {@code ResponseEntity} with a body, headers, and a status code.
124+
* @param body the entity body
125+
* @param headers the entity headers
126+
* @param statusCode the status code
134127
*/
135-
private ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, Object status) {
128+
public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatusCode statusCode) {
136129
super(body, headers);
137-
Assert.notNull(status, "HttpStatusCode must not be null");
138-
this.status = status;
130+
Assert.notNull(statusCode, "HttpStatusCode must not be null");
131+
132+
this.status = statusCode;
139133
}
140134

141135

@@ -144,12 +138,7 @@ private ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String>
144138
* @return the HTTP status as an HttpStatus enum entry
145139
*/
146140
public HttpStatusCode getStatusCode() {
147-
if (this.status instanceof HttpStatusCode statusCode) {
148-
return statusCode;
149-
}
150-
else {
151-
return HttpStatusCode.valueOf((Integer) this.status);
152-
}
141+
return this.status;
153142
}
154143

155144
/**
@@ -161,12 +150,7 @@ public HttpStatusCode getStatusCode() {
161150
*/
162151
@Deprecated(since = "6.0")
163152
public int getStatusCodeValue() {
164-
if (this.status instanceof HttpStatusCode statusCode) {
165-
return statusCode.value();
166-
}
167-
else {
168-
return (Integer) this.status;
169-
}
153+
return getStatusCode().value();
170154
}
171155

172156

@@ -530,14 +514,20 @@ public interface BodyBuilder extends HeadersBuilder<BodyBuilder> {
530514

531515
private static class DefaultBuilder implements BodyBuilder {
532516

533-
private final Object statusCode;
517+
private final HttpStatusCode statusCode;
534518

535519
private final HttpHeaders headers = new HttpHeaders();
536520

537-
public DefaultBuilder(Object statusCode) {
521+
522+
public DefaultBuilder(int statusCode) {
523+
this(HttpStatusCode.valueOf(statusCode));
524+
}
525+
526+
public DefaultBuilder(HttpStatusCode statusCode) {
538527
this.statusCode = statusCode;
539528
}
540529

530+
541531
@Override
542532
public BodyBuilder header(String headerName, String... headerValues) {
543533
for (String headerValue : headerValues) {

0 commit comments

Comments
 (0)