diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpResponse.java b/spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpResponse.java index b8356dbd8c..ab396dc920 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpResponse.java @@ -106,7 +106,7 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse { @Override @Nullable public HttpStatus getStatusCode() { - return (this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null); + return this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null; } /** diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpResponse.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpResponse.java index 20b1946277..04cb0a2b30 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpResponse.java @@ -31,6 +31,7 @@ import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.NettyDataBufferFactory; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseCookie; import org.springframework.http.ZeroCopyHttpOutputMessage; import org.springframework.util.Assert; @@ -60,12 +61,23 @@ class ReactorServerHttpResponse extends AbstractServerHttpResponse implements Ze return (T) this.response; } + @Override + @SuppressWarnings("ConstantConditions") + public HttpStatus getStatusCode() { + HttpStatus httpStatus = super.getStatusCode(); + if (httpStatus == null) { + HttpResponseStatus status = this.response.status(); + httpStatus = status != null ? HttpStatus.resolve(status.code()) : null; + } + return httpStatus; + } + @Override protected void applyStatusCode() { Integer statusCode = getStatusCodeValue(); if (statusCode != null) { - this.response.status(HttpResponseStatus.valueOf(statusCode)); + this.response.status(statusCode); } } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpResponse.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpResponse.java index 099fee14cd..9df33120f0 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,13 +34,17 @@ public interface ServerHttpResponse extends ReactiveHttpOutputMessage { /** * Set the HTTP status code of the response. * @param status the HTTP status as an {@link HttpStatus} enum value - * @return {@code false} if the status code has not been set because the HTTP response - * is already committed, {@code true} if it has been set correctly. + * @return {@code false} if the status code has not been set because the + * HTTP response is already committed, {@code true} if successfully set. */ boolean setStatusCode(@Nullable HttpStatus status); /** - * Return the HTTP status code or {@code null} if not set. + * Return the status code set via {@link #setStatusCode}, or if the status + * has not been set, return the default status code from the underlying + * server response. The return value may be {@code null} if the status code + * value is outside the {@link HttpStatus} enum range, or if the underlying + * server response does not have a default value. */ @Nullable HttpStatus getStatusCode(); diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpResponse.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpResponse.java index eae0792339..a93c89821d 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpResponse.java @@ -34,6 +34,7 @@ import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseCookie; import org.springframework.lang.Nullable; @@ -96,6 +97,12 @@ class ServletServerHttpResponse extends AbstractListenerServerHttpResponse { return (T) this.response; } + @Override + public HttpStatus getStatusCode() { + HttpStatus httpStatus = super.getStatusCode(); + return httpStatus != null ? httpStatus : HttpStatus.resolve(this.response.getStatus()); + } + @Override protected void applyStatusCode() { Integer statusCode = getStatusCodeValue(); diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpResponse.java b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpResponse.java index c0ca6d6c48..d557f286c8 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpResponse.java @@ -35,6 +35,7 @@ import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseCookie; import org.springframework.http.ZeroCopyHttpOutputMessage; import org.springframework.lang.Nullable; @@ -80,6 +81,12 @@ class UndertowServerHttpResponse extends AbstractListenerServerHttpResponse impl return (T) this.exchange; } + @Override + public HttpStatus getStatusCode() { + HttpStatus httpStatus = super.getStatusCode(); + return httpStatus != null ? httpStatus : HttpStatus.resolve(this.exchange.getStatusCode()); + } + @Override protected void applyStatusCode() {