Skip to content

Commit 7b73418

Browse files
committed
Add rawStatusCode() to ServerResponse in both WebFlux and Servlet
Closes gh-22872
1 parent 56c2987 commit 7b73418

File tree

9 files changed

+47
-2
lines changed

9 files changed

+47
-2
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ public final HttpStatus statusCode() {
349349
return HttpStatus.valueOf(this.statusCode);
350350
}
351351

352+
@Override
353+
public int rawStatusCode() {
354+
return this.statusCode;
355+
}
356+
352357
@Override
353358
public final HttpHeaders headers() {
354359
return this.headers;

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,21 @@ public interface ServerResponse {
6060

6161
/**
6262
* Return the status code of this response.
63+
* @return the status as an HttpStatus enum value
64+
* @throws IllegalArgumentException in case of an unknown HTTP status code
65+
* @see HttpStatus#valueOf(int)
6366
*/
6467
HttpStatus statusCode();
6568

69+
/**
70+
* Return the (potentially non-standard) status code of this response.
71+
* @return the status as an integer
72+
* @since 5.2
73+
* @see #statusCode()
74+
* @see HttpStatus#resolve(int)
75+
*/
76+
int rawStatusCode();
77+
6678
/**
6779
* Return the headers of this response.
6880
*/

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilderTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public void status() {
9090
String body = "foo";
9191
Mono<EntityResponse<String>> result = EntityResponse.fromObject(body).status(HttpStatus.CREATED).build();
9292
StepVerifier.create(result)
93-
.expectNextMatches(response -> HttpStatus.CREATED.equals(response.statusCode()))
93+
.expectNextMatches(response -> HttpStatus.CREATED.equals(response.statusCode()) &&
94+
response.rawStatusCode() == 201)
9495
.expectComplete()
9596
.verify();
9697
}

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public void from() {
8686
public void status() {
8787
Mono<ServerResponse> result = ServerResponse.status(HttpStatus.CREATED).build();
8888
StepVerifier.create(result)
89-
.expectNextMatches(response -> HttpStatus.CREATED.equals(response.statusCode()))
89+
.expectNextMatches(response -> HttpStatus.CREATED.equals(response.statusCode()) &&
90+
response.rawStatusCode() == 201)
9091
.expectComplete()
9192
.verify();
9293
}

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionsTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ public HttpStatus statusCode() {
195195
return HttpStatus.OK;
196196
}
197197
@Override
198+
public int rawStatusCode() {
199+
return 200;
200+
}
201+
@Override
198202
public HttpHeaders headers() {
199203
return new HttpHeaders();
200204
}
@@ -229,6 +233,10 @@ public HttpStatus statusCode() {
229233
return HttpStatus.OK;
230234
}
231235
@Override
236+
public int rawStatusCode() {
237+
return 200;
238+
}
239+
@Override
232240
public HttpHeaders headers() {
233241
return new HttpHeaders();
234242
}

spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerResponseBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ public final HttpStatus statusCode() {
264264
return HttpStatus.valueOf(this.statusCode);
265265
}
266266

267+
@Override
268+
public int rawStatusCode() {
269+
return this.statusCode;
270+
}
271+
267272
@Override
268273
public final HttpHeaders headers() {
269274
return this.headers;

spring-webmvc/src/main/java/org/springframework/web/servlet/function/ServerResponse.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,20 @@ public interface ServerResponse {
5757

5858
/**
5959
* Return the status code of this response.
60+
* @return the status as an HttpStatus enum value
61+
* @throws IllegalArgumentException in case of an unknown HTTP status code
62+
* @see HttpStatus#valueOf(int)
6063
*/
6164
HttpStatus statusCode();
6265

66+
/**
67+
* Return the (potentially non-standard) status code of this response.
68+
* @return the status as an integer
69+
* @see #statusCode()
70+
* @see HttpStatus#valueOf(int)
71+
*/
72+
int rawStatusCode();
73+
6374
/**
6475
* Return the headers of this response.
6576
*/

spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultEntityResponseBuilderTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public void status() {
8181
EntityResponse.fromObject(body).status(HttpStatus.CREATED).build();
8282

8383
assertThat(result.statusCode()).isEqualTo(HttpStatus.CREATED);
84+
assertThat(result.rawStatusCode()).isEqualTo(201);
8485
}
8586

8687
@Test

spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultServerResponseBuilderTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public List<HttpMessageConverter<?>> messageConverters() {
6767
public void status() {
6868
ServerResponse response = ServerResponse.status(HttpStatus.CREATED).build();
6969
assertThat(response.statusCode()).isEqualTo(HttpStatus.CREATED);
70+
assertThat(response.rawStatusCode()).isEqualTo(201);
7071
}
7172

7273
@Test

0 commit comments

Comments
 (0)