Skip to content

Commit 3890d4c

Browse files
committed
AbstractServerHttpResponse stores HTTP status code as integer value
Issue: SPR-16073
1 parent 8a94077 commit 3890d4c

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpResponse.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private enum State {NEW, COMMITTING, COMMITTED}
6262
private final DataBufferFactory dataBufferFactory;
6363

6464
@Nullable
65-
private HttpStatus statusCode;
65+
private Integer statusCode;
6666

6767
private final HttpHeaders headers;
6868

@@ -96,14 +96,33 @@ public boolean setStatusCode(@Nullable HttpStatus statusCode) {
9696
return false;
9797
}
9898
else {
99-
this.statusCode = statusCode;
99+
this.statusCode = (statusCode != null ? statusCode.value() : null);
100100
return true;
101101
}
102102
}
103103

104104
@Override
105105
@Nullable
106106
public HttpStatus getStatusCode() {
107+
return (this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null);
108+
}
109+
110+
/**
111+
* Set the HTTP status code of the response.
112+
* @param statusCode the HTTP status as an integer value
113+
* @since 5.0.1
114+
*/
115+
public void setStatusCodeValue(Integer statusCode) {
116+
this.statusCode = statusCode;
117+
}
118+
119+
/**
120+
* Return the HTTP status code of the response.
121+
* @return the HTTP status as an integer value
122+
* @since 5.0.1
123+
*/
124+
@Nullable
125+
public Integer getStatusCodeValue() {
107126
return this.statusCode;
108127
}
109128

@@ -121,7 +140,7 @@ public MultiValueMap<String, ResponseCookie> getCookies() {
121140

122141
@Override
123142
public void addCookie(ResponseCookie cookie) {
124-
Assert.notNull(cookie, "'cookie' must not be null");
143+
Assert.notNull(cookie, "ResponseCookie must not be null");
125144

126145
if (this.state.get() == State.COMMITTED) {
127146
throw new IllegalStateException("Can't add the cookie " + cookie +

spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpResponse.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.springframework.core.io.buffer.DataBuffer;
3333
import org.springframework.core.io.buffer.DataBufferFactory;
3434
import org.springframework.core.io.buffer.NettyDataBufferFactory;
35-
import org.springframework.http.HttpStatus;
3635
import org.springframework.http.ResponseCookie;
3736
import org.springframework.http.ZeroCopyHttpOutputMessage;
3837
import org.springframework.util.Assert;
@@ -65,9 +64,9 @@ public <T> T getNativeResponse() {
6564

6665
@Override
6766
protected void applyStatusCode() {
68-
HttpStatus statusCode = this.getStatusCode();
67+
Integer statusCode = getStatusCodeValue();
6968
if (statusCode != null) {
70-
this.response.status(HttpResponseStatus.valueOf(statusCode.value()));
69+
this.response.status(HttpResponseStatus.valueOf(statusCode));
7170
}
7271
}
7372

spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public <T> T getNativeResponse() {
9292

9393
@Override
9494
protected void applyStatusCode() {
95-
HttpStatus statusCode = this.getStatusCode();
95+
Integer statusCode = getStatusCodeValue();
9696
if (statusCode != null) {
97-
this.response.setStatus(statusCode.value());
97+
this.response.setStatus(statusCode);
9898
}
9999
}
100100

spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpResponse.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.springframework.core.io.buffer.DataBuffer;
3838
import org.springframework.core.io.buffer.DataBufferFactory;
3939
import org.springframework.core.io.buffer.DataBufferUtils;
40-
import org.springframework.http.HttpStatus;
4140
import org.springframework.http.ResponseCookie;
4241
import org.springframework.http.ZeroCopyHttpOutputMessage;
4342
import org.springframework.lang.Nullable;
@@ -75,9 +74,9 @@ public <T> T getNativeResponse() {
7574

7675
@Override
7776
protected void applyStatusCode() {
78-
HttpStatus statusCode = this.getStatusCode();
77+
Integer statusCode = getStatusCodeValue();
7978
if (statusCode != null) {
80-
this.exchange.setStatusCode(statusCode.value());
79+
this.exchange.setStatusCode(statusCode);
8180
}
8281
}
8382

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.springframework.http.RequestEntity;
3333
import org.springframework.http.ResponseEntity;
3434
import org.springframework.http.codec.HttpMessageWriter;
35+
import org.springframework.http.server.reactive.AbstractServerHttpResponse;
36+
import org.springframework.http.server.reactive.ServerHttpResponse;
3537
import org.springframework.lang.Nullable;
3638
import org.springframework.util.Assert;
3739
import org.springframework.web.reactive.HandlerResult;
@@ -48,8 +50,7 @@
4850
* @author Rossen Stoyanchev
4951
* @since 5.0
5052
*/
51-
public class ResponseEntityResultHandler extends AbstractMessageWriterResultHandler
52-
implements HandlerResultHandler {
53+
public class ResponseEntityResultHandler extends AbstractMessageWriterResultHandler implements HandlerResultHandler {
5354

5455
private static final List<HttpMethod> SAFE_METHODS = Arrays.asList(HttpMethod.GET, HttpMethod.HEAD);
5556

@@ -139,7 +140,13 @@ else if (returnValue instanceof HttpHeaders) {
139140

140141
if (httpEntity instanceof ResponseEntity) {
141142
ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity;
142-
exchange.getResponse().setStatusCode(responseEntity.getStatusCode());
143+
ServerHttpResponse response = exchange.getResponse();
144+
if (response instanceof AbstractServerHttpResponse) {
145+
((AbstractServerHttpResponse) response).setStatusCodeValue(responseEntity.getStatusCodeValue());
146+
}
147+
else {
148+
response.setStatusCode(responseEntity.getStatusCode());
149+
}
143150
}
144151

145152
HttpHeaders entityHeaders = httpEntity.getHeaders();

0 commit comments

Comments
 (0)