Skip to content

Commit 75b1396

Browse files
committed
Fall back on default server response status code
Update the ServerHttpRespnose contract to indicate that server specific sub-classes should fall back on the default status, if a status code has not been set explicitly. Issue: SPR-17368
1 parent 4182935 commit 75b1396

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public boolean setStatusCode(@Nullable HttpStatus status) {
106106
@Override
107107
@Nullable
108108
public HttpStatus getStatusCode() {
109-
return (this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null);
109+
return this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null;
110110
}
111111

112112
/**

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.core.io.buffer.DataBufferFactory;
3232
import org.springframework.core.io.buffer.NettyDataBufferFactory;
3333
import org.springframework.http.HttpHeaders;
34+
import org.springframework.http.HttpStatus;
3435
import org.springframework.http.ResponseCookie;
3536
import org.springframework.http.ZeroCopyHttpOutputMessage;
3637
import org.springframework.util.Assert;
@@ -60,12 +61,23 @@ public <T> T getNativeResponse() {
6061
return (T) this.response;
6162
}
6263

64+
@Override
65+
@SuppressWarnings("ConstantConditions")
66+
public HttpStatus getStatusCode() {
67+
HttpStatus httpStatus = super.getStatusCode();
68+
if (httpStatus == null) {
69+
HttpResponseStatus status = this.response.status();
70+
httpStatus = status != null ? HttpStatus.resolve(status.code()) : null;
71+
}
72+
return httpStatus;
73+
}
74+
6375

6476
@Override
6577
protected void applyStatusCode() {
6678
Integer statusCode = getStatusCodeValue();
6779
if (statusCode != null) {
68-
this.response.status(HttpResponseStatus.valueOf(statusCode));
80+
this.response.status(statusCode);
6981
}
7082
}
7183

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,13 +34,17 @@ public interface ServerHttpResponse extends ReactiveHttpOutputMessage {
3434
/**
3535
* Set the HTTP status code of the response.
3636
* @param status the HTTP status as an {@link HttpStatus} enum value
37-
* @return {@code false} if the status code has not been set because the HTTP response
38-
* is already committed, {@code true} if it has been set correctly.
37+
* @return {@code false} if the status code has not been set because the
38+
* HTTP response is already committed, {@code true} if successfully set.
3939
*/
4040
boolean setStatusCode(@Nullable HttpStatus status);
4141

4242
/**
43-
* Return the HTTP status code or {@code null} if not set.
43+
* Return the status code set via {@link #setStatusCode}, or if the status
44+
* has not been set, return the default status code from the underlying
45+
* server response. The return value may be {@code null} if the status code
46+
* value is outside the {@link HttpStatus} enum range, or if the underlying
47+
* server response does not have a default value.
4448
*/
4549
@Nullable
4650
HttpStatus getStatusCode();

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.core.io.buffer.DataBufferFactory;
3535
import org.springframework.core.io.buffer.DataBufferUtils;
3636
import org.springframework.http.HttpHeaders;
37+
import org.springframework.http.HttpStatus;
3738
import org.springframework.http.MediaType;
3839
import org.springframework.http.ResponseCookie;
3940
import org.springframework.lang.Nullable;
@@ -96,6 +97,12 @@ public <T> T getNativeResponse() {
9697
return (T) this.response;
9798
}
9899

100+
@Override
101+
public HttpStatus getStatusCode() {
102+
HttpStatus httpStatus = super.getStatusCode();
103+
return httpStatus != null ? httpStatus : HttpStatus.resolve(this.response.getStatus());
104+
}
105+
99106
@Override
100107
protected void applyStatusCode() {
101108
Integer statusCode = getStatusCodeValue();

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.core.io.buffer.DataBufferFactory;
3636
import org.springframework.core.io.buffer.DataBufferUtils;
3737
import org.springframework.http.HttpHeaders;
38+
import org.springframework.http.HttpStatus;
3839
import org.springframework.http.ResponseCookie;
3940
import org.springframework.http.ZeroCopyHttpOutputMessage;
4041
import org.springframework.lang.Nullable;
@@ -80,6 +81,12 @@ public <T> T getNativeResponse() {
8081
return (T) this.exchange;
8182
}
8283

84+
@Override
85+
public HttpStatus getStatusCode() {
86+
HttpStatus httpStatus = super.getStatusCode();
87+
return httpStatus != null ? httpStatus : HttpStatus.resolve(this.exchange.getStatusCode());
88+
}
89+
8390

8491
@Override
8592
protected void applyStatusCode() {

0 commit comments

Comments
 (0)