Use HttpStatusCode interface

This commit contains changes made because of the introduction of
HttpStatusCode. In general, methods that used to return a HttpStatus
now return HttpStatusCode instead, and methods that returned raw status
codes are now deprecated.

See gh-28214
This commit is contained in:
Arjen Poutsma
2022-03-17 14:21:04 +01:00
parent ca4b6e86a4
commit 28ac0d3883
143 changed files with 1064 additions and 950 deletions

View File

@@ -55,7 +55,7 @@ public class ProblemDetail {
/**
* Protected constructor for subclasses.
* <p>To create a {@link ProblemDetail} instance, use static factory methods,
* {@link #forStatus(HttpStatus)} or {@link #forRawStatusCode(int)}.
* {@link #forStatus(HttpStatusCode)} or {@link #forStatus(int)}.
* @param rawStatusCode the response status to use
*/
protected ProblemDetail(int rawStatusCode) {
@@ -97,12 +97,12 @@ public class ProblemDetail {
/**
* Variant of {@link #setStatus(int)} for chained initialization.
* @param status the response status for the problem
* @param statusCode the response status for the problem
* @return the same instance
*/
public ProblemDetail withStatus(HttpStatus status) {
Assert.notNull(status, "HttpStatus is required");
setStatus(status.value());
public ProblemDetail withStatus(HttpStatusCode statusCode) {
Assert.notNull(statusCode, "HttpStatus is required");
setStatus(statusCode.value());
return this;
}
@@ -111,7 +111,7 @@ public class ProblemDetail {
* @param status the response status value for the problem
* @return the same instance
*/
public ProblemDetail withRawStatusCode(int status) {
public ProblemDetail withStatus(int status) {
setStatus(status);
return this;
}
@@ -164,8 +164,8 @@ public class ProblemDetail {
/**
* Setter for the {@link #getStatus() problem status}.
* @param status the problem status
* @see #withStatus(HttpStatus)
* @see #withRawStatusCode(int)
* @see #withStatus(HttpStatusCode)
* @see #withStatus(int)
*/
public void setStatus(int status) {
this.status = status;
@@ -264,15 +264,15 @@ public class ProblemDetail {
/**
* Create a {@code ProblemDetail} instance with the given status code.
*/
public static ProblemDetail forStatus(HttpStatus status) {
Assert.notNull(status, "HttpStatus is required");
return forRawStatusCode(status.value());
public static ProblemDetail forStatus(HttpStatusCode status) {
Assert.notNull(status, "HttpStatusCode is required");
return forStatus(status.value());
}
/**
* Create a {@code ProblemDetail} instance with the given status value.
*/
public static ProblemDetail forRawStatusCode(int status) {
public static ProblemDetail forStatus(int status) {
return new ProblemDetail(status);
}

View File

@@ -31,7 +31,7 @@ import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
/**
* Extension of {@link HttpEntity} that adds an {@link HttpStatus} status code.
* Extension of {@link HttpEntity} that adds an {@link HttpStatusCode} status code.
* Used in {@code RestTemplate} as well as in {@code @Controller} methods.
*
* <p>In {@code RestTemplate}, this class is returned by
@@ -85,7 +85,7 @@ public class ResponseEntity<T> extends HttpEntity<T> {
* Create a {@code ResponseEntity} with a status code only.
* @param status the status code
*/
public ResponseEntity(HttpStatus status) {
public ResponseEntity(HttpStatusCode status) {
this(null, null, status);
}
@@ -94,7 +94,7 @@ public class ResponseEntity<T> extends HttpEntity<T> {
* @param body the entity body
* @param status the status code
*/
public ResponseEntity(@Nullable T body, HttpStatus status) {
public ResponseEntity(@Nullable T body, HttpStatusCode status) {
this(body, null, status);
}
@@ -103,7 +103,7 @@ public class ResponseEntity<T> extends HttpEntity<T> {
* @param headers the entity headers
* @param status the status code
*/
public ResponseEntity(MultiValueMap<String, String> headers, HttpStatus status) {
public ResponseEntity(MultiValueMap<String, String> headers, HttpStatusCode status) {
this(null, headers, status);
}
@@ -113,7 +113,7 @@ public class ResponseEntity<T> extends HttpEntity<T> {
* @param headers the entity headers
* @param status the status code
*/
public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatus status) {
public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatusCode status) {
this(body, headers, (Object) status);
}
@@ -133,7 +133,7 @@ public class ResponseEntity<T> extends HttpEntity<T> {
*/
private ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, Object status) {
super(body, headers);
Assert.notNull(status, "HttpStatus must not be null");
Assert.notNull(status, "HttpStatusCode must not be null");
this.status = status;
}
@@ -142,12 +142,12 @@ public class ResponseEntity<T> extends HttpEntity<T> {
* Return the HTTP status code of the response.
* @return the HTTP status as an HttpStatus enum entry
*/
public HttpStatus getStatusCode() {
if (this.status instanceof HttpStatus) {
return (HttpStatus) this.status;
public HttpStatusCode getStatusCode() {
if (this.status instanceof HttpStatusCode statusCode) {
return statusCode;
}
else {
return HttpStatus.valueOf((Integer) this.status);
return HttpStatusCode.valueOf((Integer) this.status);
}
}
@@ -155,10 +155,12 @@ public class ResponseEntity<T> extends HttpEntity<T> {
* Return the HTTP status code of the response.
* @return the HTTP status as an int value
* @since 4.3
* @deprecated as of 6.0, in favor of {@link #getStatusCode()}
*/
@Deprecated
public int getStatusCodeValue() {
if (this.status instanceof HttpStatus) {
return ((HttpStatus) this.status).value();
if (this.status instanceof HttpStatusCode statusCode) {
return statusCode.value();
}
else {
return (Integer) this.status;
@@ -187,9 +189,9 @@ public class ResponseEntity<T> extends HttpEntity<T> {
public String toString() {
StringBuilder builder = new StringBuilder("<");
builder.append(this.status);
if (this.status instanceof HttpStatus) {
if (this.status instanceof HttpStatus httpStatus) {
builder.append(' ');
builder.append(((HttpStatus) this.status).getReasonPhrase());
builder.append(httpStatus.getReasonPhrase());
}
builder.append(',');
T body = getBody();
@@ -212,8 +214,8 @@ public class ResponseEntity<T> extends HttpEntity<T> {
* @return the created builder
* @since 4.1
*/
public static BodyBuilder status(HttpStatus status) {
Assert.notNull(status, "HttpStatus must not be null");
public static BodyBuilder status(HttpStatusCode status) {
Assert.notNull(status, "HttpStatusCode must not be null");
return new DefaultBuilder(status);
}

View File

@@ -18,19 +18,21 @@ package org.springframework.http.client;
import java.io.IOException;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
/**
* Abstract base for {@link ClientHttpResponse}.
*
* @author Arjen Poutsma
* @since 3.1.1
* @deprecated with no direct replacement
*/
@Deprecated
public abstract class AbstractClientHttpResponse implements ClientHttpResponse {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.valueOf(getRawStatusCode());
public HttpStatusCode getStatusCode() throws IOException {
return HttpStatusCode.valueOf(getRawStatusCode());
}
}

View File

@@ -21,7 +21,7 @@ import java.io.IOException;
import java.io.InputStream;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
import org.springframework.util.StreamUtils;
@@ -46,11 +46,12 @@ final class BufferingClientHttpResponseWrapper implements ClientHttpResponse {
@Override
public HttpStatus getStatusCode() throws IOException {
public HttpStatusCode getStatusCode() throws IOException {
return this.response.getStatusCode();
}
@Override
@Deprecated
public int getRawStatusCode() throws IOException {
return this.response.getRawStatusCode();
}

View File

@@ -20,7 +20,7 @@ import java.io.Closeable;
import java.io.IOException;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
/**
* Represents a client-side HTTP response.
@@ -36,26 +36,21 @@ import org.springframework.http.HttpStatus;
public interface ClientHttpResponse extends HttpInputMessage, Closeable {
/**
* Get the HTTP status code as an {@link HttpStatus} enum value.
* <p>For status codes not supported by {@code HttpStatus}, use
* {@link #getRawStatusCode()} instead.
* @return the HTTP status as an HttpStatus enum value (never {@code null})
* Get the HTTP status code as an {@link HttpStatusCode}.
* @return the HTTP status as {@code HttpStatusCode} value (never {@code null})
* @throws IOException in case of I/O errors
* @throws IllegalArgumentException in case of an unknown HTTP status code
* @since #getRawStatusCode()
* @see HttpStatus#valueOf(int)
*/
HttpStatus getStatusCode() throws IOException;
HttpStatusCode getStatusCode() throws IOException;
/**
* Get the HTTP status code (potentially non-standard and not
* resolvable through the {@link HttpStatus} enum) as an integer.
* Get the HTTP status code as an integer.
* @return the HTTP status as an integer value
* @throws IOException in case of I/O errors
* @since 3.1.1
* @see #getStatusCode()
* @see HttpStatus#resolve(int)
* @deprecated as of 6.0, in favor of {@link #getStatusCode()}
*/
@Deprecated
int getRawStatusCode() throws IOException;
/**

View File

@@ -26,6 +26,7 @@ import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
import org.springframework.util.StreamUtils;
@@ -40,7 +41,7 @@ import org.springframework.util.StreamUtils;
* @since 3.1
* @see HttpComponentsClientHttpRequest#execute()
*/
final class HttpComponentsClientHttpResponse extends AbstractClientHttpResponse {
final class HttpComponentsClientHttpResponse implements ClientHttpResponse {
private final HttpResponse httpResponse;
@@ -54,6 +55,12 @@ final class HttpComponentsClientHttpResponse extends AbstractClientHttpResponse
@Override
public HttpStatusCode getStatusCode() throws IOException {
return HttpStatusCode.valueOf(this.httpResponse.getStatusLine().getStatusCode());
}
@Override
@Deprecated
public int getRawStatusCode() throws IOException {
return this.httpResponse.getStatusLine().getStatusCode();
}

View File

@@ -23,6 +23,7 @@ import okhttp3.Response;
import okhttp3.ResponseBody;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;
@@ -35,7 +36,7 @@ import org.springframework.util.StreamUtils;
* @author Roy Clarkson
* @since 4.3
*/
class OkHttp3ClientHttpResponse extends AbstractClientHttpResponse {
class OkHttp3ClientHttpResponse implements ClientHttpResponse {
private final Response response;
@@ -50,6 +51,12 @@ class OkHttp3ClientHttpResponse extends AbstractClientHttpResponse {
@Override
public HttpStatusCode getStatusCode() throws IOException {
return HttpStatusCode.valueOf(this.response.code());
}
@Override
@Deprecated
public int getRawStatusCode() {
return this.response.code();
}

View File

@@ -21,6 +21,7 @@ import java.io.InputStream;
import java.net.HttpURLConnection;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
@@ -34,7 +35,7 @@ import org.springframework.util.StringUtils;
* @author Brian Clozel
* @since 3.0
*/
final class SimpleClientHttpResponse extends AbstractClientHttpResponse {
final class SimpleClientHttpResponse implements ClientHttpResponse {
private final HttpURLConnection connection;
@@ -51,6 +52,12 @@ final class SimpleClientHttpResponse extends AbstractClientHttpResponse {
@Override
public HttpStatusCode getStatusCode() throws IOException {
return HttpStatusCode.valueOf(this.connection.getResponseCode());
}
@Override
@Deprecated
public int getRawStatusCode() throws IOException {
return this.connection.getResponseCode();
}

View File

@@ -16,7 +16,7 @@
package org.springframework.http.client.reactive;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ReactiveHttpInputMessage;
import org.springframework.http.ResponseCookie;
import org.springframework.util.MultiValueMap;
@@ -41,22 +41,19 @@ public interface ClientHttpResponse extends ReactiveHttpInputMessage {
}
/**
* Return the HTTP status code as an {@link HttpStatus} enum value.
* @return the HTTP status as an HttpStatus enum value (never {@code null})
* @throws IllegalArgumentException in case of an unknown HTTP status code
* @since #getRawStatusCode()
* @see HttpStatus#valueOf(int)
* Return the HTTP status code as an {@link HttpStatusCode}.
* @return the HTTP status as {@code HttpStatusCode} value (never {@code null})
*/
HttpStatus getStatusCode();
HttpStatusCode getStatusCode();
/**
* Return the HTTP status code (potentially non-standard and not
* resolvable through the {@link HttpStatus} enum) as an integer.
* Return the HTTP status code as an integer.
* @return the HTTP status as an integer value
* @since 5.0.6
* @see #getStatusCode()
* @see HttpStatus#resolve(int)
* @deprecated as of 6.0, in favor of {@link #getStatusCode()}
*/
@Deprecated
int getRawStatusCode();
/**

View File

@@ -20,7 +20,7 @@ import reactor.core.publisher.Flux;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseCookie;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;
@@ -56,11 +56,12 @@ public class ClientHttpResponseDecorator implements ClientHttpResponse {
}
@Override
public HttpStatus getStatusCode() {
public HttpStatusCode getStatusCode() {
return this.delegate.getStatusCode();
}
@Override
@Deprecated
public int getRawStatusCode() {
return this.delegate.getRawStatusCode();
}

View File

@@ -29,7 +29,7 @@ import reactor.core.publisher.Flux;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseCookie;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@@ -68,11 +68,12 @@ class HttpComponentsClientHttpResponse implements ClientHttpResponse {
@Override
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(this.message.getHead().getCode());
public HttpStatusCode getStatusCode() {
return HttpStatusCode.valueOf(this.message.getHead().getCode());
}
@Override
@Deprecated
public int getRawStatusCode() {
return this.message.getHead().getCode();
}

View File

@@ -35,7 +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.HttpStatusCode;
import org.springframework.http.ResponseCookie;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
@@ -80,11 +80,12 @@ class JdkClientHttpResponse implements ClientHttpResponse {
@Override
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(this.response.statusCode());
public HttpStatusCode getStatusCode() {
return HttpStatusCode.valueOf(this.response.statusCode());
}
@Override
@Deprecated
public int getRawStatusCode() {
return this.response.statusCode();
}

View File

@@ -27,7 +27,7 @@ import reactor.core.publisher.Flux;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseCookie;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
@@ -64,11 +64,12 @@ class JettyClientHttpResponse implements ClientHttpResponse {
@Override
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(getRawStatusCode());
public HttpStatusCode getStatusCode() {
return HttpStatusCode.valueOf(this.reactiveResponse.getStatus());
}
@Override
@Deprecated
public int getRawStatusCode() {
return this.reactiveResponse.getStatus();
}

View File

@@ -34,7 +34,7 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseCookie;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
@@ -135,11 +135,12 @@ class ReactorClientHttpResponse implements ClientHttpResponse {
}
@Override
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(getRawStatusCode());
public HttpStatusCode getStatusCode() {
return HttpStatusCode.valueOf(this.response.status().code());
}
@Override
@Deprecated
public int getRawStatusCode() {
return this.response.status().code();
}

View File

@@ -20,7 +20,7 @@ import java.io.IOException;
import java.io.OutputStream;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.util.Assert;
/**
@@ -52,7 +52,7 @@ public class DelegatingServerHttpResponse implements ServerHttpResponse {
}
@Override
public void setStatusCode(HttpStatus status) {
public void setStatusCode(HttpStatusCode status) {
this.delegate.setStatusCode(status);
}

View File

@@ -21,7 +21,7 @@ import java.io.Flushable;
import java.io.IOException;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
/**
* Represents a server-side HTTP response.
@@ -35,7 +35,7 @@ public interface ServerHttpResponse extends HttpOutputMessage, Flushable, Closea
* Set the HTTP status code of the response.
* @param status the HTTP status as an HttpStatus enum value
*/
void setStatusCode(HttpStatus status);
void setStatusCode(HttpStatusCode status);
/**
* Ensure that the headers and the content of the response are written out.

View File

@@ -26,7 +26,7 @@ import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
@@ -71,7 +71,7 @@ public class ServletServerHttpResponse implements ServerHttpResponse {
}
@Override
public void setStatusCode(HttpStatus status) {
public void setStatusCode(HttpStatusCode status) {
Assert.notNull(status, "HttpStatus must not be null");
this.servletResponse.setStatus(status.value());
}

View File

@@ -31,7 +31,7 @@ import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.PooledDataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseCookie;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -62,7 +62,7 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
private final DataBufferFactory dataBufferFactory;
@Nullable
private Integer statusCode;
private HttpStatusCode statusCode;
private final HttpHeaders headers;
@@ -95,37 +95,32 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
}
@Override
public boolean setStatusCode(@Nullable HttpStatus status) {
public boolean setStatusCode(@Nullable HttpStatusCode status) {
if (this.state.get() == State.COMMITTED) {
return false;
}
else {
this.statusCode = (status != null ? status.value() : null);
this.statusCode = status;
return true;
}
}
@Override
@Nullable
public HttpStatus getStatusCode() {
return (this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null);
public HttpStatusCode getStatusCode() {
return this.statusCode;
}
@Override
public boolean setRawStatusCode(@Nullable Integer statusCode) {
if (this.state.get() == State.COMMITTED) {
return false;
}
else {
this.statusCode = statusCode;
return true;
}
return setStatusCode(statusCode != null ? HttpStatusCode.valueOf(statusCode) : null);
}
@Override
@Nullable
@Deprecated
public Integer getRawStatusCode() {
return this.statusCode;
return this.statusCode != null ? this.statusCode.value() : null;
}
@Override

View File

@@ -34,7 +34,7 @@ import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.util.Assert;
@@ -68,12 +68,13 @@ class ReactorServerHttpResponse extends AbstractServerHttpResponse implements Ze
}
@Override
public HttpStatus getStatusCode() {
HttpStatus status = super.getStatusCode();
return (status != null ? status : HttpStatus.resolve(this.response.status().code()));
public HttpStatusCode getStatusCode() {
HttpStatusCode status = super.getStatusCode();
return (status != null ? status : HttpStatusCode.valueOf(this.response.status().code()));
}
@Override
@Deprecated
public Integer getRawStatusCode() {
Integer status = super.getRawStatusCode();
return (status != null ? status : this.response.status().code());
@@ -81,9 +82,9 @@ class ReactorServerHttpResponse extends AbstractServerHttpResponse implements Ze
@Override
protected void applyStatusCode() {
Integer status = super.getRawStatusCode();
HttpStatusCode status = super.getStatusCode();
if (status != null) {
this.response.status(status);
this.response.status(status.value());
}
}

View File

@@ -16,7 +16,7 @@
package org.springframework.http.server.reactive;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ReactiveHttpOutputMessage;
import org.springframework.http.ResponseCookie;
import org.springframework.lang.Nullable;
@@ -34,42 +34,30 @@ public interface ServerHttpResponse extends ReactiveHttpOutputMessage {
/**
* Set the HTTP status code of the response.
* @param status the HTTP status as an {@link HttpStatus} enum value
* @param status the HTTP status as an {@link HttpStatusCode} value
* @return {@code false} if the status code change wasn't processed because
* the HTTP response is committed, {@code true} if successfully set.
*/
boolean setStatusCode(@Nullable HttpStatus status);
boolean setStatusCode(@Nullable HttpStatusCode status);
/**
* Return the status code that has been set, or otherwise fall back on the
* status of the response from the underlying server. The return value may
* be {@code null} if the status code value is outside the
* {@link HttpStatus} enum range, or if there is no default value from the
* be {@code null} if there is no default value from the
* underlying server.
*/
@Nullable
HttpStatus getStatusCode();
HttpStatusCode getStatusCode();
/**
* Set the HTTP status code to the given value (potentially non-standard and
* not resolvable through the {@link HttpStatus} enum) as an integer.
* Set the HTTP status code to the given value as an integer.
* @param value the status code value
* @return {@code false} if the status code change wasn't processed because
* the HTTP response is committed, {@code true} if successfully set.
* @since 5.2.4
*/
default boolean setRawStatusCode(@Nullable Integer value) {
if (value == null) {
return setStatusCode(null);
}
else {
HttpStatus httpStatus = HttpStatus.resolve(value);
if (httpStatus == null) {
throw new IllegalStateException(
"Unresolvable HttpStatus for general ServerHttpResponse: " + value);
}
return setStatusCode(httpStatus);
}
return setStatusCode(value != null ? HttpStatusCode.valueOf(value) : null);
}
/**
@@ -77,10 +65,12 @@ public interface ServerHttpResponse extends ReactiveHttpOutputMessage {
* status of the response from the underlying server. The return value may
* be {@code null} if there is no default value from the underlying server.
* @since 5.2.4
* @deprecated as of 6.0, in favor of {@link #getStatusCode()}
*/
@Nullable
@Deprecated
default Integer getRawStatusCode() {
HttpStatus httpStatus = getStatusCode();
HttpStatusCode httpStatus = getStatusCode();
return (httpStatus != null ? httpStatus.value() : null);
}

View File

@@ -24,7 +24,7 @@ import reactor.core.publisher.Mono;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseCookie;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -56,12 +56,12 @@ public class ServerHttpResponseDecorator implements ServerHttpResponse {
// ServerHttpResponse delegation methods...
@Override
public boolean setStatusCode(@Nullable HttpStatus status) {
public boolean setStatusCode(@Nullable HttpStatusCode status) {
return getDelegate().setStatusCode(status);
}
@Override
public HttpStatus getStatusCode() {
public HttpStatusCode getStatusCode() {
return getDelegate().getStatusCode();
}
@@ -71,6 +71,7 @@ public class ServerHttpResponseDecorator implements ServerHttpResponse {
}
@Override
@Deprecated
public Integer getRawStatusCode() {
return getDelegate().getRawStatusCode();
}

View File

@@ -34,7 +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.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie;
import org.springframework.lang.Nullable;
@@ -101,12 +101,13 @@ class ServletServerHttpResponse extends AbstractListenerServerHttpResponse {
}
@Override
public HttpStatus getStatusCode() {
HttpStatus status = super.getStatusCode();
return (status != null ? status : HttpStatus.resolve(this.response.getStatus()));
public HttpStatusCode getStatusCode() {
HttpStatusCode status = super.getStatusCode();
return (status != null ? status : HttpStatusCode.valueOf(this.response.getStatus()));
}
@Override
@Deprecated
public Integer getRawStatusCode() {
Integer status = super.getRawStatusCode();
return (status != null ? status : this.response.getStatus());
@@ -114,9 +115,9 @@ class ServletServerHttpResponse extends AbstractListenerServerHttpResponse {
@Override
protected void applyStatusCode() {
Integer status = super.getRawStatusCode();
HttpStatusCode status = super.getStatusCode();
if (status != null) {
this.response.setStatus(status);
this.response.setStatus(status.value());
}
}

View File

@@ -35,7 +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.HttpStatusCode;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.lang.Nullable;
@@ -81,12 +81,13 @@ class UndertowServerHttpResponse extends AbstractListenerServerHttpResponse impl
}
@Override
public HttpStatus getStatusCode() {
HttpStatus status = super.getStatusCode();
return (status != null ? status : HttpStatus.resolve(this.exchange.getStatusCode()));
public HttpStatusCode getStatusCode() {
HttpStatusCode status = super.getStatusCode();
return (status != null ? status : HttpStatusCode.valueOf(this.exchange.getStatusCode()));
}
@Override
@Deprecated
public Integer getRawStatusCode() {
Integer status = super.getRawStatusCode();
return (status != null ? status : this.exchange.getStatusCode());
@@ -94,9 +95,9 @@ class UndertowServerHttpResponse extends AbstractListenerServerHttpResponse impl
@Override
protected void applyStatusCode() {
Integer status = super.getRawStatusCode();
HttpStatusCode status = super.getStatusCode();
if (status != null) {
this.exchange.setStatusCode(status);
this.exchange.setStatusCode(status.value());
}
}

View File

@@ -17,7 +17,7 @@
package org.springframework.web;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
@@ -41,18 +41,9 @@ import org.springframework.http.ProblemDetail;
public interface ErrorResponse {
/**
* Return the HTTP status to use for the response.
* @throws IllegalArgumentException for an unknown HTTP status code
* Return the HTTP status code to use for the response.
*/
default HttpStatus getStatus() {
return HttpStatus.valueOf(getRawStatusCode());
}
/**
* Return the HTTP status value for the response, potentially non-standard
* and not resolvable via {@link HttpStatus}.
*/
int getRawStatusCode();
HttpStatusCode getStatusCode();
/**
* Return headers to use for the response.

View File

@@ -16,13 +16,12 @@
package org.springframework.web;
import java.net.URI;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.core.NestedRuntimeException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.lang.Nullable;
@@ -43,7 +42,7 @@ import org.springframework.lang.Nullable;
@SuppressWarnings("serial")
public class ErrorResponseException extends NestedRuntimeException implements ErrorResponse {
private final int status;
private final HttpStatusCode status;
private final HttpHeaders headers = new HttpHeaders();
@@ -51,40 +50,31 @@ public class ErrorResponseException extends NestedRuntimeException implements Er
/**
* Constructor with a well-known {@link HttpStatus}.
* Constructor with a {@link HttpStatusCode}.
*/
public ErrorResponseException(HttpStatus status) {
public ErrorResponseException(HttpStatusCode status) {
this(status, null);
}
/**
* Constructor with a well-known {@link HttpStatus} and an optional cause.
* Constructor with a {@link HttpStatusCode} and an optional cause.
*/
public ErrorResponseException(HttpStatus status, @Nullable Throwable cause) {
this(status.value(), cause);
}
/**
* Constructor that accepts any status value, possibly not resolvable as an
* {@link HttpStatus} enum, and an optional cause.
*/
public ErrorResponseException(int status, @Nullable Throwable cause) {
this(status, ProblemDetail.forRawStatusCode(status), cause);
public ErrorResponseException(HttpStatusCode status, @Nullable Throwable cause) {
this(status, ProblemDetail.forStatus(status), cause);
}
/**
* Constructor with a given {@link ProblemDetail} instance, possibly a
* subclass of {@code ProblemDetail} with extended fields.
*/
public ErrorResponseException(int status, ProblemDetail body, @Nullable Throwable cause) {
public ErrorResponseException(HttpStatusCode status, ProblemDetail body, @Nullable Throwable cause) {
super(null, cause);
this.status = status;
this.body = body;
}
@Override
public int getRawStatusCode() {
public HttpStatusCode getStatusCode() {
return this.status;
}
@@ -147,9 +137,7 @@ public class ErrorResponseException extends NestedRuntimeException implements Er
@Override
public String getMessage() {
HttpStatus httpStatus = HttpStatus.resolve(this.status);
String message = (httpStatus != null ? httpStatus : String.valueOf(this.status)) +
(!this.headers.isEmpty() ? ", headers=" + this.headers : "") + ", " + this.body;
String message = this.status + (!this.headers.isEmpty() ? ", headers=" + this.headers : "") + ", " + this.body;
return NestedExceptionUtils.buildMessage(message, getCause());
}

View File

@@ -35,7 +35,7 @@ public abstract class HttpMediaTypeException extends ServletException implements
private final List<MediaType> supportedMediaTypes;
private final ProblemDetail body = ProblemDetail.forRawStatusCode(getRawStatusCode());
private final ProblemDetail body = ProblemDetail.forStatus(getStatusCode());
/**

View File

@@ -21,6 +21,7 @@ import java.util.stream.Collectors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
@@ -55,8 +56,8 @@ public class HttpMediaTypeNotAcceptableException extends HttpMediaTypeException
@Override
public int getRawStatusCode() {
return HttpStatus.NOT_ACCEPTABLE.value();
public HttpStatusCode getStatusCode() {
return HttpStatus.NOT_ACCEPTABLE;
}
@Override

View File

@@ -21,6 +21,7 @@ import java.util.List;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
@@ -104,8 +105,8 @@ public class HttpMediaTypeNotSupportedException extends HttpMediaTypeException {
}
@Override
public int getRawStatusCode() {
return HttpStatus.UNSUPPORTED_MEDIA_TYPE.value();
public HttpStatusCode getStatusCode() {
return HttpStatus.UNSUPPORTED_MEDIA_TYPE;
}
@Override

View File

@@ -25,6 +25,7 @@ import jakarta.servlet.ServletException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
@@ -95,7 +96,7 @@ public class HttpRequestMethodNotSupportedException extends ServletException imp
this.supportedMethods = supportedMethods;
String detail = "Method '" + method + "' is not supported.";
this.body = ProblemDetail.forRawStatusCode(getRawStatusCode()).withDetail(detail);
this.body = ProblemDetail.forStatus(getStatusCode()).withDetail(detail);
}
@@ -133,8 +134,8 @@ public class HttpRequestMethodNotSupportedException extends ServletException imp
}
@Override
public int getRawStatusCode() {
return HttpStatus.METHOD_NOT_ALLOWED.value();
public HttpStatusCode getStatusCode() {
return HttpStatus.METHOD_NOT_ALLOWED;
}
@Override

View File

@@ -18,6 +18,7 @@ package org.springframework.web.bind;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
@@ -48,13 +49,12 @@ public class MethodArgumentNotValidException extends BindException implements Er
public MethodArgumentNotValidException(MethodParameter parameter, BindingResult bindingResult) {
super(bindingResult);
this.parameter = parameter;
this.body = ProblemDetail.forRawStatusCode(getRawStatusCode()).withDetail("Invalid request content.");
this.body = ProblemDetail.forStatus(getStatusCode()).withDetail("Invalid request content.");
}
@Override
public int getRawStatusCode() {
return HttpStatus.BAD_REQUEST.value();
public HttpStatusCode getStatusCode() {
return HttpStatus.BAD_REQUEST;
}
@Override

View File

@@ -18,6 +18,7 @@ package org.springframework.web.bind;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
/**
* {@link ServletRequestBindingException} subclass that indicates that a path
@@ -85,10 +86,9 @@ public class MissingPathVariableException extends MissingRequestValueException {
return this.parameter;
}
@Override
public int getRawStatusCode() {
return HttpStatus.INTERNAL_SERVER_ERROR.value();
public HttpStatusCode getStatusCode() {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}

View File

@@ -17,6 +17,7 @@
package org.springframework.web.bind;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.web.ErrorResponse;
import org.springframework.web.util.NestedServletException;
@@ -35,7 +36,7 @@ import org.springframework.web.util.NestedServletException;
@SuppressWarnings("serial")
public class ServletRequestBindingException extends NestedServletException implements ErrorResponse {
private final ProblemDetail body = ProblemDetail.forRawStatusCode(getRawStatusCode());
private final ProblemDetail body = ProblemDetail.forStatus(getStatusCode());
/**
@@ -55,10 +56,9 @@ public class ServletRequestBindingException extends NestedServletException imple
super(msg, cause);
}
@Override
public int getRawStatusCode() {
return HttpStatus.BAD_REQUEST.value();
public HttpStatusCode getStatusCode() {
return HttpStatus.BAD_REQUEST;
}
@Override

View File

@@ -23,6 +23,7 @@ import java.nio.charset.StandardCharsets;
import org.springframework.core.log.LogFormatUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.lang.Nullable;
@@ -35,7 +36,7 @@ import org.springframework.util.ObjectUtils;
* <p>This error handler checks for the status code on the
* {@link ClientHttpResponse}. Any code in the 4xx or 5xx series is considered
* to be an error. This behavior can be changed by overriding
* {@link #hasError(HttpStatus)}. Unknown status codes will be ignored by
* {@link #hasError(HttpStatusCode)}. Unknown status codes will be ignored by
* {@link #hasError(ClientHttpResponse)}.
*
* <p>See {@link #handleError(ClientHttpResponse)} for more details on specific
@@ -50,28 +51,25 @@ import org.springframework.util.ObjectUtils;
public class DefaultResponseErrorHandler implements ResponseErrorHandler {
/**
* Delegates to {@link #hasError(HttpStatus)} (for a standard status enum value) or
* {@link #hasError(int)} (for an unknown status code) with the response status code.
* @see ClientHttpResponse#getRawStatusCode()
* @see #hasError(HttpStatus)
* @see #hasError(int)
* Delegates to {@link #hasError(HttpStatusCode)} with the response status code.
* @see ClientHttpResponse#getStatusCode()
* @see #hasError(HttpStatusCode)
*/
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
int rawStatusCode = response.getRawStatusCode();
HttpStatus statusCode = HttpStatus.resolve(rawStatusCode);
return (statusCode != null ? hasError(statusCode) : hasError(rawStatusCode));
HttpStatusCode statusCode = response.getStatusCode();
return hasError(statusCode);
}
/**
* Template method called from {@link #hasError(ClientHttpResponse)}.
* <p>The default implementation checks {@link HttpStatus#isError()}.
* <p>The default implementation checks {@link HttpStatusCode#isError()}.
* Can be overridden in subclasses.
* @param statusCode the HTTP status code as enum value
* @param statusCode the HTTP status code
* @return {@code true} if the response indicates an error; {@code false} otherwise
* @see HttpStatus#isError()
* @see HttpStatusCode#isError()
*/
protected boolean hasError(HttpStatus statusCode) {
protected boolean hasError(HttpStatusCode statusCode) {
return statusCode.isError();
}
@@ -81,14 +79,16 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
* {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR CLIENT_ERROR} or
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR SERVER_ERROR}.
* Can be overridden in subclasses.
* @param unknownStatusCode the HTTP status code as raw value
* @param statusCode the HTTP status code as raw value
* @return {@code true} if the response indicates an error; {@code false} otherwise
* @since 4.3.21
* @see org.springframework.http.HttpStatus.Series#CLIENT_ERROR
* @see org.springframework.http.HttpStatus.Series#SERVER_ERROR
* @deprecated in favor of {@link #hasError(HttpStatusCode)}
*/
protected boolean hasError(int unknownStatusCode) {
HttpStatus.Series series = HttpStatus.Series.resolve(unknownStatusCode);
@Deprecated
protected boolean hasError(int statusCode) {
HttpStatus.Series series = HttpStatus.Series.resolve(statusCode);
return (series == HttpStatus.Series.CLIENT_ERROR || series == HttpStatus.Series.SERVER_ERROR);
}
@@ -106,19 +106,11 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
* {@link HttpStatus} enum range.
* </ul>
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code
* @see #handleError(ClientHttpResponse, HttpStatus)
* @see #handleError(ClientHttpResponse, HttpStatusCode)
*/
@Override
public void handleError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode());
if (statusCode == null) {
byte[] body = getResponseBody(response);
String message = getErrorMessage(response.getRawStatusCode(),
response.getStatusText(), body, getCharset(response));
throw new UnknownHttpStatusCodeException(message,
response.getRawStatusCode(), response.getStatusText(),
response.getHeaders(), body, getCharset(response));
}
HttpStatusCode statusCode = response.getStatusCode();
handleError(response, statusCode);
}
@@ -156,20 +148,21 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
* @see HttpClientErrorException#create
* @see HttpServerErrorException#create
*/
protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
protected void handleError(ClientHttpResponse response, HttpStatusCode statusCode) throws IOException {
String statusText = response.getStatusText();
HttpHeaders headers = response.getHeaders();
byte[] body = getResponseBody(response);
Charset charset = getCharset(response);
String message = getErrorMessage(statusCode.value(), statusText, body, charset);
switch (statusCode.series()) {
case CLIENT_ERROR ->
throw HttpClientErrorException.create(message, statusCode, statusText, headers, body, charset);
case SERVER_ERROR ->
throw HttpServerErrorException.create(message, statusCode, statusText, headers, body, charset);
default ->
throw new UnknownHttpStatusCodeException(message, statusCode.value(), statusText, headers, body, charset);
if (statusCode.is4xxClientError()) {
throw HttpClientErrorException.create(message, statusCode, statusText, headers, body, charset);
}
else if (statusCode.is5xxServerError()) {
throw HttpServerErrorException.create(message, statusCode, statusText, headers, body, charset);
}
else {
throw new UnknownHttpStatusCodeException(message, statusCode.value(), statusText, headers, body, charset);
}
}

View File

@@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.Nullable;
@@ -59,7 +60,7 @@ public class ExtractingResponseErrorHandler extends DefaultResponseErrorHandler
private List<HttpMessageConverter<?>> messageConverters = Collections.emptyList();
private final Map<HttpStatus, Class<? extends RestClientException>> statusMapping = new LinkedHashMap<>();
private final Map<HttpStatusCode, Class<? extends RestClientException>> statusMapping = new LinkedHashMap<>();
private final Map<HttpStatus.Series, Class<? extends RestClientException>> seriesMapping = new LinkedHashMap<>();
@@ -97,7 +98,7 @@ public class ExtractingResponseErrorHandler extends DefaultResponseErrorHandler
* {@linkplain #setMessageConverters(List) configured message converters} to convert the
* response into the mapped subclass of {@link RestClientException}.
*/
public void setStatusMapping(Map<HttpStatus, Class<? extends RestClientException>> statusMapping) {
public void setStatusMapping(Map<HttpStatusCode, Class<? extends RestClientException>> statusMapping) {
if (!CollectionUtils.isEmpty(statusMapping)) {
this.statusMapping.putAll(statusMapping);
}
@@ -120,12 +121,13 @@ public class ExtractingResponseErrorHandler extends DefaultResponseErrorHandler
@Override
protected boolean hasError(HttpStatus statusCode) {
protected boolean hasError(HttpStatusCode statusCode) {
if (this.statusMapping.containsKey(statusCode)) {
return this.statusMapping.get(statusCode) != null;
}
else if (this.seriesMapping.containsKey(statusCode.series())) {
return this.seriesMapping.get(statusCode.series()) != null;
HttpStatus.Series series = HttpStatus.Series.resolve(statusCode.value());
if (this.seriesMapping.containsKey(series)) {
return this.seriesMapping.get(series) != null;
}
else {
return super.hasError(statusCode);
@@ -133,12 +135,13 @@ public class ExtractingResponseErrorHandler extends DefaultResponseErrorHandler
}
@Override
public void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
public void handleError(ClientHttpResponse response, HttpStatusCode statusCode) throws IOException {
if (this.statusMapping.containsKey(statusCode)) {
extract(this.statusMapping.get(statusCode), response);
}
else if (this.seriesMapping.containsKey(statusCode.series())) {
extract(this.seriesMapping.get(statusCode.series()), response);
HttpStatus.Series series = HttpStatus.Series.resolve(statusCode.value());
if (this.seriesMapping.containsKey(series)) {
extract(this.seriesMapping.get(series), response);
}
else {
super.handleError(response, statusCode);

View File

@@ -20,6 +20,7 @@ import java.nio.charset.Charset;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
/**
@@ -37,14 +38,14 @@ public class HttpClientErrorException extends HttpStatusCodeException {
/**
* Constructor with a status code only.
*/
public HttpClientErrorException(HttpStatus statusCode) {
public HttpClientErrorException(HttpStatusCode statusCode) {
super(statusCode);
}
/**
* Constructor with a status code and status text.
*/
public HttpClientErrorException(HttpStatus statusCode, String statusText) {
public HttpClientErrorException(HttpStatusCode statusCode, String statusText) {
super(statusCode, statusText);
}
@@ -52,7 +53,7 @@ public class HttpClientErrorException extends HttpStatusCodeException {
* Constructor with a status code and status text, and content.
*/
public HttpClientErrorException(
HttpStatus statusCode, String statusText, @Nullable byte[] body, @Nullable Charset responseCharset) {
HttpStatusCode statusCode, String statusText, @Nullable byte[] body, @Nullable Charset responseCharset) {
super(statusCode, statusText, body, responseCharset);
}
@@ -60,7 +61,7 @@ public class HttpClientErrorException extends HttpStatusCodeException {
/**
* Constructor with a status code and status text, headers, and content.
*/
public HttpClientErrorException(HttpStatus statusCode, String statusText,
public HttpClientErrorException(HttpStatusCode statusCode, String statusText,
@Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset responseCharset) {
super(statusCode, statusText, headers, body, responseCharset);
@@ -71,7 +72,7 @@ public class HttpClientErrorException extends HttpStatusCodeException {
* and an prepared message.
* @since 5.2.2
*/
public HttpClientErrorException(String message, HttpStatus statusCode, String statusText,
public HttpClientErrorException(String message, HttpStatusCode statusCode, String statusText,
@Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset responseCharset) {
super(message, statusCode, statusText, headers, body, responseCharset);
@@ -83,68 +84,64 @@ public class HttpClientErrorException extends HttpStatusCodeException {
* @since 5.1
*/
public static HttpClientErrorException create(
HttpStatus statusCode, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
HttpStatusCode statusCode, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
return create(null, statusCode, statusText, headers, body, charset);
}
/**
* Variant of {@link #create(HttpStatus, String, HttpHeaders, byte[], Charset)}
* Variant of {@link #create(HttpStatusCode, String, HttpHeaders, byte[], Charset)}
* with an optional prepared message.
* @since 5.2.2
*/
public static HttpClientErrorException create(@Nullable String message, HttpStatus statusCode,
public static HttpClientErrorException create(@Nullable String message, HttpStatusCode statusCode,
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
switch (statusCode) {
case BAD_REQUEST:
return message != null ?
new HttpClientErrorException.BadRequest(message, statusText, headers, body, charset) :
new HttpClientErrorException.BadRequest(statusText, headers, body, charset);
case UNAUTHORIZED:
return message != null ?
new HttpClientErrorException.Unauthorized(message, statusText, headers, body, charset) :
new HttpClientErrorException.Unauthorized(statusText, headers, body, charset);
case FORBIDDEN:
return message != null ?
new HttpClientErrorException.Forbidden(message, statusText, headers, body, charset) :
new HttpClientErrorException.Forbidden(statusText, headers, body, charset);
case NOT_FOUND:
return message != null ?
new HttpClientErrorException.NotFound(message, statusText, headers, body, charset) :
new HttpClientErrorException.NotFound(statusText, headers, body, charset);
case METHOD_NOT_ALLOWED:
return message != null ?
new HttpClientErrorException.MethodNotAllowed(message, statusText, headers, body, charset) :
new HttpClientErrorException.MethodNotAllowed(statusText, headers, body, charset);
case NOT_ACCEPTABLE:
return message != null ?
new HttpClientErrorException.NotAcceptable(message, statusText, headers, body, charset) :
new HttpClientErrorException.NotAcceptable(statusText, headers, body, charset);
case CONFLICT:
return message != null ?
new HttpClientErrorException.Conflict(message, statusText, headers, body, charset) :
new HttpClientErrorException.Conflict(statusText, headers, body, charset);
case GONE:
return message != null ?
new HttpClientErrorException.Gone(message, statusText, headers, body, charset) :
new HttpClientErrorException.Gone(statusText, headers, body, charset);
case UNSUPPORTED_MEDIA_TYPE:
return message != null ?
new HttpClientErrorException.UnsupportedMediaType(message, statusText, headers, body, charset) :
new HttpClientErrorException.UnsupportedMediaType(statusText, headers, body, charset);
case TOO_MANY_REQUESTS:
return message != null ?
new HttpClientErrorException.TooManyRequests(message, statusText, headers, body, charset) :
new HttpClientErrorException.TooManyRequests(statusText, headers, body, charset);
case UNPROCESSABLE_ENTITY:
return message != null ?
new HttpClientErrorException.UnprocessableEntity(message, statusText, headers, body, charset) :
new HttpClientErrorException.UnprocessableEntity(statusText, headers, body, charset);
default:
return message != null ?
if (statusCode instanceof HttpStatus status) {
return switch (status) {
case BAD_REQUEST -> message != null ?
new BadRequest(message, statusText, headers, body, charset) :
new BadRequest(statusText, headers, body, charset);
case UNAUTHORIZED -> message != null ?
new Unauthorized(message, statusText, headers, body, charset) :
new Unauthorized(statusText, headers, body, charset);
case FORBIDDEN -> message != null ?
new Forbidden(message, statusText, headers, body, charset) :
new Forbidden(statusText, headers, body, charset);
case NOT_FOUND -> message != null ?
new NotFound(message, statusText, headers, body, charset) :
new NotFound(statusText, headers, body, charset);
case METHOD_NOT_ALLOWED -> message != null ?
new MethodNotAllowed(message, statusText, headers, body, charset) :
new MethodNotAllowed(statusText, headers, body, charset);
case NOT_ACCEPTABLE -> message != null ?
new NotAcceptable(message, statusText, headers, body, charset) :
new NotAcceptable(statusText, headers, body, charset);
case CONFLICT -> message != null ?
new Conflict(message, statusText, headers, body, charset) :
new Conflict(statusText, headers, body, charset);
case GONE -> message != null ?
new Gone(message, statusText, headers, body, charset) :
new Gone(statusText, headers, body, charset);
case UNSUPPORTED_MEDIA_TYPE -> message != null ?
new UnsupportedMediaType(message, statusText, headers, body, charset) :
new UnsupportedMediaType(statusText, headers, body, charset);
case TOO_MANY_REQUESTS -> message != null ?
new TooManyRequests(message, statusText, headers, body, charset) :
new TooManyRequests(statusText, headers, body, charset);
case UNPROCESSABLE_ENTITY -> message != null ?
new UnprocessableEntity(message, statusText, headers, body, charset) :
new UnprocessableEntity(statusText, headers, body, charset);
default -> message != null ?
new HttpClientErrorException(message, statusCode, statusText, headers, body, charset) :
new HttpClientErrorException(statusCode, statusText, headers, body, charset);
};
}
if (message != null) {
return new HttpClientErrorException(message, statusCode, statusText, headers, body, charset);
}
else {
return new HttpClientErrorException(statusCode, statusText, headers, body, charset);
}
}

View File

@@ -122,7 +122,7 @@ public class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> {
}
throw new UnknownContentTypeException(this.responseType, contentType,
responseWrapper.getRawStatusCode(), responseWrapper.getStatusText(),
responseWrapper.getStatusCode(), responseWrapper.getStatusText(),
responseWrapper.getHeaders(), getResponseBody(responseWrapper));
}

View File

@@ -20,6 +20,7 @@ import java.nio.charset.Charset;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
/**
@@ -37,14 +38,14 @@ public class HttpServerErrorException extends HttpStatusCodeException {
/**
* Constructor with a status code only.
*/
public HttpServerErrorException(HttpStatus statusCode) {
public HttpServerErrorException(HttpStatusCode statusCode) {
super(statusCode);
}
/**
* Constructor with a status code and status text.
*/
public HttpServerErrorException(HttpStatus statusCode, String statusText) {
public HttpServerErrorException(HttpStatusCode statusCode, String statusText) {
super(statusCode, statusText);
}
@@ -52,7 +53,7 @@ public class HttpServerErrorException extends HttpStatusCodeException {
* Constructor with a status code and status text, and content.
*/
public HttpServerErrorException(
HttpStatus statusCode, String statusText, @Nullable byte[] body, @Nullable Charset charset) {
HttpStatusCode statusCode, String statusText, @Nullable byte[] body, @Nullable Charset charset) {
super(statusCode, statusText, body, charset);
}
@@ -60,7 +61,7 @@ public class HttpServerErrorException extends HttpStatusCodeException {
/**
* Constructor with a status code and status text, headers, and content.
*/
public HttpServerErrorException(HttpStatus statusCode, String statusText,
public HttpServerErrorException(HttpStatusCode statusCode, String statusText,
@Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset charset) {
super(statusCode, statusText, headers, body, charset);
@@ -71,7 +72,7 @@ public class HttpServerErrorException extends HttpStatusCodeException {
* prepared message.
* @since 5.2.2
*/
public HttpServerErrorException(String message, HttpStatus statusCode, String statusText,
public HttpServerErrorException(String message, HttpStatusCode statusCode, String statusText,
@Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset charset) {
super(message, statusCode, statusText, headers, body, charset);
@@ -81,46 +82,55 @@ public class HttpServerErrorException extends HttpStatusCodeException {
* Create an {@code HttpServerErrorException} or an HTTP status specific sub-class.
* @since 5.1
*/
public static HttpServerErrorException create(HttpStatus statusCode,
public static HttpServerErrorException create(HttpStatusCode statusCode,
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
return create(null, statusCode, statusText, headers, body, charset);
}
/**
* Variant of {@link #create(String, HttpStatus, String, HttpHeaders, byte[], Charset)}
* Variant of {@link #create(String, HttpStatusCode, String, HttpHeaders, byte[], Charset)}
* with an optional prepared message.
* @since 5.2.2.
*/
public static HttpServerErrorException create(@Nullable String message, HttpStatus statusCode,
public static HttpServerErrorException create(@Nullable String message, HttpStatusCode statusCode,
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
switch (statusCode) {
case INTERNAL_SERVER_ERROR:
return message != null ?
if (statusCode instanceof HttpStatus status) {
switch (status) {
case INTERNAL_SERVER_ERROR:
return message != null ?
new HttpServerErrorException.InternalServerError(message, statusText, headers, body, charset) :
new HttpServerErrorException.InternalServerError(statusText, headers, body, charset);
case NOT_IMPLEMENTED:
return message != null ?
new HttpServerErrorException.NotImplemented(message, statusText, headers, body, charset) :
new HttpServerErrorException.NotImplemented(statusText, headers, body, charset);
case BAD_GATEWAY:
return message != null ?
new HttpServerErrorException.BadGateway(message, statusText, headers, body, charset) :
new HttpServerErrorException.BadGateway(statusText, headers, body, charset);
case SERVICE_UNAVAILABLE:
return message != null ?
new HttpServerErrorException.InternalServerError(statusText, headers, body, charset);
case NOT_IMPLEMENTED:
return message != null ?
new HttpServerErrorException.NotImplemented(message, statusText, headers, body, charset) :
new HttpServerErrorException.NotImplemented(statusText, headers, body, charset);
case BAD_GATEWAY:
return message != null ?
new HttpServerErrorException.BadGateway(message, statusText, headers, body, charset) :
new HttpServerErrorException.BadGateway(statusText, headers, body, charset);
case SERVICE_UNAVAILABLE:
return message != null ?
new HttpServerErrorException.ServiceUnavailable(message, statusText, headers, body, charset) :
new HttpServerErrorException.ServiceUnavailable(statusText, headers, body, charset);
case GATEWAY_TIMEOUT:
return message != null ?
new HttpServerErrorException.GatewayTimeout(message, statusText, headers, body, charset) :
new HttpServerErrorException.GatewayTimeout(statusText, headers, body, charset);
default:
return message != null ?
new HttpServerErrorException(message, statusCode, statusText, headers, body, charset) :
new HttpServerErrorException(statusCode, statusText, headers, body, charset);
new HttpServerErrorException.ServiceUnavailable(statusText, headers, body, charset);
case GATEWAY_TIMEOUT:
return message != null ?
new HttpServerErrorException.GatewayTimeout(message, statusText, headers, body, charset) :
new HttpServerErrorException.GatewayTimeout(statusText, headers, body, charset);
default:
return message != null ?
new HttpServerErrorException(message, statusCode, statusText, headers, body, charset) :
new HttpServerErrorException(statusCode, statusText, headers, body, charset);
}
}
if (message != null) {
return new HttpServerErrorException(message, statusCode, statusText, headers, body, charset);
}
else {
return new HttpServerErrorException(statusCode, statusText, headers, body, charset);
}
}

View File

@@ -20,11 +20,12 @@ import java.nio.charset.Charset;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/**
* Abstract base class for exceptions based on an {@link HttpStatus}.
* Abstract base class for exceptions based on an {@link HttpStatusCode}.
*
* @author Arjen Poutsma
* @author Chris Beams
@@ -36,42 +37,48 @@ public abstract class HttpStatusCodeException extends RestClientResponseExceptio
private static final long serialVersionUID = 5696801857651587810L;
private final HttpStatus statusCode;
/**
* Construct a new instance with an {@link HttpStatus}.
* Construct a new instance with an {@link HttpStatusCode}.
* @param statusCode the status code
*/
protected HttpStatusCodeException(HttpStatus statusCode) {
this(statusCode, statusCode.name(), null, null, null);
protected HttpStatusCodeException(HttpStatusCode statusCode) {
this(statusCode, name(statusCode), null, null, null);
}
private static String name(HttpStatusCode statusCode) {
if (statusCode instanceof HttpStatus status) {
return status.name();
}
else {
return "";
}
}
/**
* Construct a new instance with an {@link HttpStatus} and status text.
* Construct a new instance with an {@link HttpStatusCode} and status text.
* @param statusCode the status code
* @param statusText the status text
*/
protected HttpStatusCodeException(HttpStatus statusCode, String statusText) {
protected HttpStatusCodeException(HttpStatusCode statusCode, String statusText) {
this(statusCode, statusText, null, null, null);
}
/**
* Construct instance with an {@link HttpStatus}, status text, and content.
* Construct instance with an {@link HttpStatusCode}, status text, and content.
* @param statusCode the status code
* @param statusText the status text
* @param responseBody the response body content, may be {@code null}
* @param responseCharset the response body charset, may be {@code null}
* @since 3.0.5
*/
protected HttpStatusCodeException(HttpStatus statusCode, String statusText,
protected HttpStatusCodeException(HttpStatusCode statusCode, String statusText,
@Nullable byte[] responseBody, @Nullable Charset responseCharset) {
this(statusCode, statusText, null, responseBody, responseCharset);
}
/**
* Construct instance with an {@link HttpStatus}, status text, content, and
* Construct instance with an {@link HttpStatusCode}, status text, content, and
* a response charset.
* @param statusCode the status code
* @param statusText the status text
@@ -80,7 +87,7 @@ public abstract class HttpStatusCodeException extends RestClientResponseExceptio
* @param responseCharset the response body charset, may be {@code null}
* @since 3.1.2
*/
protected HttpStatusCodeException(HttpStatus statusCode, String statusText,
protected HttpStatusCodeException(HttpStatusCode statusCode, String statusText,
@Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) {
this(getMessage(statusCode, statusText),
@@ -88,7 +95,7 @@ public abstract class HttpStatusCodeException extends RestClientResponseExceptio
}
/**
* Construct instance with an {@link HttpStatus}, status text, content, and
* Construct instance with an {@link HttpStatusCode}, status text, content, and
* a response charset.
* @param message the exception message
* @param statusCode the status code
@@ -98,25 +105,17 @@ public abstract class HttpStatusCodeException extends RestClientResponseExceptio
* @param responseCharset the response body charset, may be {@code null}
* @since 5.2.2
*/
protected HttpStatusCodeException(String message, HttpStatus statusCode, String statusText,
protected HttpStatusCodeException(String message, HttpStatusCode statusCode, String statusText,
@Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) {
super(message, statusCode.value(), statusText, responseHeaders, responseBody, responseCharset);
this.statusCode = statusCode;
super(message, statusCode, statusText, responseHeaders, responseBody, responseCharset);
}
private static String getMessage(HttpStatus statusCode, String statusText) {
if (!StringUtils.hasLength(statusText)) {
statusText = statusCode.getReasonPhrase();
private static String getMessage(HttpStatusCode statusCode, String statusText) {
if (!StringUtils.hasLength(statusText) && statusCode instanceof HttpStatus status) {
statusText = status.getReasonPhrase();
}
return statusCode.value() + " " + statusText;
}
/**
* Return the HTTP status code.
*/
public HttpStatus getStatusCode() {
return this.statusCode;
}
}

View File

@@ -22,6 +22,7 @@ import java.io.PushbackInputStream;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.lang.Nullable;
@@ -58,9 +59,9 @@ class MessageBodyClientHttpResponseWrapper implements ClientHttpResponse {
* @throws IOException in case of I/O errors
*/
public boolean hasMessageBody() throws IOException {
HttpStatus status = HttpStatus.resolve(getRawStatusCode());
if (status != null && (status.is1xxInformational() || status == HttpStatus.NO_CONTENT ||
status == HttpStatus.NOT_MODIFIED)) {
HttpStatusCode statusCode = getStatusCode();
if (statusCode.is1xxInformational() || statusCode == HttpStatus.NO_CONTENT ||
statusCode == HttpStatus.NOT_MODIFIED) {
return false;
}
if (getHeaders().getContentLength() == 0) {
@@ -121,11 +122,12 @@ class MessageBodyClientHttpResponseWrapper implements ClientHttpResponse {
}
@Override
public HttpStatus getStatusCode() throws IOException {
public HttpStatusCode getStatusCode() throws IOException {
return this.response.getStatusCode();
}
@Override
@Deprecated
public int getRawStatusCode() throws IOException {
return this.response.getRawStatusCode();
}

View File

@@ -21,6 +21,7 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
/**
@@ -36,7 +37,7 @@ public class RestClientResponseException extends RestClientException {
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private final int rawStatusCode;
private final HttpStatusCode statusCode;
private final String statusText;
@@ -59,9 +60,22 @@ public class RestClientResponseException extends RestClientException {
*/
public RestClientResponseException(String message, int statusCode, String statusText,
@Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) {
this(message, HttpStatusCode.valueOf(statusCode), statusText, responseHeaders, responseBody, responseCharset);
}
/**
* Construct a new instance of with the given response data.
* @param statusCode the raw status code value
* @param statusText the status text
* @param responseHeaders the response headers (may be {@code null})
* @param responseBody the response body content (may be {@code null})
* @param responseCharset the response body charset (may be {@code null})
* @since 6.0
*/
public RestClientResponseException(String message, HttpStatusCode statusCode, String statusText,
@Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) {
super(message);
this.rawStatusCode = statusCode;
this.statusCode = statusCode;
this.statusText = statusText;
this.responseHeaders = responseHeaders;
this.responseBody = (responseBody != null ? responseBody : new byte[0]);
@@ -70,10 +84,20 @@ public class RestClientResponseException extends RestClientException {
/**
* Return the raw HTTP status code value.
* Return the HTTP status code.
* @since 6.0
*/
public HttpStatusCode getStatusCode() {
return this.statusCode;
}
/**
* Return the raw HTTP status code value.
* @deprecated as of 6.0, in favor of {@link #getStatusCode()}
*/
@Deprecated
public int getRawStatusCode() {
return this.rawStatusCode;
return this.statusCode.value();
}
/**

View File

@@ -33,7 +33,7 @@ import org.springframework.core.SpringProperties;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
@@ -798,9 +798,8 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
boolean hasError = errorHandler.hasError(response);
if (logger.isDebugEnabled()) {
try {
int code = response.getRawStatusCode();
HttpStatus status = HttpStatus.resolve(code);
logger.debug("Response " + (status != null ? status : code));
HttpStatusCode statusCode = response.getStatusCode();
logger.debug("Response " + statusCode);
}
catch (IOException ex) {
// ignore
@@ -1026,10 +1025,10 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
public ResponseEntity<T> extractData(ClientHttpResponse response) throws IOException {
if (this.delegate != null) {
T body = this.delegate.extractData(response);
return ResponseEntity.status(response.getRawStatusCode()).headers(response.getHeaders()).body(body);
return ResponseEntity.status(response.getStatusCode()).headers(response.getHeaders()).body(body);
}
else {
return ResponseEntity.status(response.getRawStatusCode()).headers(response.getHeaders()).build();
return ResponseEntity.status(response.getStatusCode()).headers(response.getHeaders()).build();
}
}
}

View File

@@ -20,6 +20,7 @@ import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
@@ -40,7 +41,7 @@ public class UnknownContentTypeException extends RestClientException {
private final MediaType contentType;
private final int rawStatusCode;
private final HttpStatusCode statusCode;
private final String statusText;
@@ -61,12 +62,28 @@ public class UnknownContentTypeException extends RestClientException {
public UnknownContentTypeException(Type targetType, MediaType contentType,
int statusCode, String statusText, HttpHeaders responseHeaders, byte[] responseBody) {
this(targetType, contentType, HttpStatusCode.valueOf(statusCode), statusText, responseHeaders, responseBody);
}
/**
* Construct a new instance of with the given response data.
* @param targetType the expected target type
* @param contentType the content type of the response
* @param statusCode the raw status code value
* @param statusText the status text
* @param responseHeaders the response headers (may be {@code null})
* @param responseBody the response body content (may be {@code null})
* @since 6.0
*/
public UnknownContentTypeException(Type targetType, MediaType contentType,
HttpStatusCode statusCode, String statusText, HttpHeaders responseHeaders, byte[] responseBody) {
super("Could not extract response: no suitable HttpMessageConverter found " +
"for response type [" + targetType + "] and content type [" + contentType + "]");
this.targetType = targetType;
this.contentType = contentType;
this.rawStatusCode = statusCode;
this.statusCode = statusCode;
this.statusText = statusText;
this.responseHeaders = responseHeaders;
this.responseBody = responseBody;
@@ -88,10 +105,19 @@ public class UnknownContentTypeException extends RestClientException {
}
/**
* Return the raw HTTP status code value.
* Return the HTTP status code value.
*/
public HttpStatusCode getStatusCode() {
return this.statusCode;
}
/**
* Return the raw HTTP status code value.
* @deprecated as of 6.0, in favor of {@link #getStatusCode()}
*/
@Deprecated
public int getRawStatusCode() {
return this.rawStatusCode;
return this.statusCode.value();
}
/**

View File

@@ -19,7 +19,6 @@ package org.springframework.web.client;
import java.nio.charset.Charset;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;
/**
@@ -34,8 +33,8 @@ public class UnknownHttpStatusCodeException extends RestClientResponseException
/**
* Construct a new instance of {@code HttpStatusCodeException} based on an
* {@link HttpStatus}, status text, and response body content.
* Construct a new instance of {@code HttpStatusCodeException} based on a
* status code, status text, and response body content.
* @param rawStatusCode the raw status code value
* @param statusText the status text
* @param responseHeaders the response headers (may be {@code null})
@@ -50,8 +49,8 @@ public class UnknownHttpStatusCodeException extends RestClientResponseException
}
/**
* Construct a new instance of {@code HttpStatusCodeException} based on an
* {@link HttpStatus}, status text, and response body content.
* Construct a new instance of {@code HttpStatusCodeException} based on a
* status code, status text, and response body content.
* @param rawStatusCode the raw status code value
* @param statusText the status text
* @param responseHeaders the response headers (may be {@code null})

View File

@@ -17,6 +17,7 @@
package org.springframework.web.context.request.async;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.web.ErrorResponse;
@@ -37,13 +38,13 @@ import org.springframework.web.ErrorResponse;
public class AsyncRequestTimeoutException extends RuntimeException implements ErrorResponse {
@Override
public int getRawStatusCode() {
return HttpStatus.SERVICE_UNAVAILABLE.value();
public HttpStatusCode getStatusCode() {
return HttpStatus.SERVICE_UNAVAILABLE;
}
@Override
public ProblemDetail getBody() {
return ProblemDetail.forRawStatusCode(getRawStatusCode());
return ProblemDetail.forStatus(getStatusCode());
}
}

View File

@@ -24,6 +24,7 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.util.Assert;
/**
@@ -43,7 +44,7 @@ import org.springframework.util.Assert;
*/
public class RelativeRedirectFilter extends OncePerRequestFilter {
private HttpStatus redirectStatus = HttpStatus.SEE_OTHER;
private HttpStatusCode redirectStatus = HttpStatus.SEE_OTHER;
/**
@@ -51,7 +52,7 @@ public class RelativeRedirectFilter extends OncePerRequestFilter {
* <p>By default this is {@link HttpStatus#SEE_OTHER}.
* @param status the 3xx redirect status to use
*/
public void setRedirectStatus(HttpStatus status) {
public void setRedirectStatus(HttpStatusCode status) {
Assert.notNull(status, "Property 'redirectStatus' is required");
Assert.isTrue(status.is3xxRedirection(), "Not a redirect status code");
this.redirectStatus = status;
@@ -60,7 +61,7 @@ public class RelativeRedirectFilter extends OncePerRequestFilter {
/**
* Return the configured redirect status.
*/
public HttpStatus getRedirectStatus() {
public HttpStatusCode getRedirectStatus() {
return this.redirectStatus;
}

View File

@@ -20,7 +20,7 @@ import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponseWrapper;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.util.Assert;
import org.springframework.web.util.WebUtils;
@@ -33,10 +33,10 @@ import org.springframework.web.util.WebUtils;
*/
final class RelativeRedirectResponseWrapper extends HttpServletResponseWrapper {
private final HttpStatus redirectStatus;
private final HttpStatusCode redirectStatus;
private RelativeRedirectResponseWrapper(HttpServletResponse response, HttpStatus redirectStatus) {
private RelativeRedirectResponseWrapper(HttpServletResponse response, HttpStatusCode redirectStatus) {
super(response);
Assert.notNull(redirectStatus, "'redirectStatus' is required");
this.redirectStatus = redirectStatus;
@@ -51,7 +51,7 @@ final class RelativeRedirectResponseWrapper extends HttpServletResponseWrapper {
public static HttpServletResponse wrapIfNecessary(HttpServletResponse response,
HttpStatus redirectStatus) {
HttpStatusCode redirectStatus) {
RelativeRedirectResponseWrapper wrapper =
WebUtils.getNativeResponse(response, RelativeRedirectResponseWrapper.class);

View File

@@ -36,7 +36,7 @@ import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.SynthesizingMethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -85,7 +85,7 @@ public class HandlerMethod {
private final MethodParameter[] parameters;
@Nullable
private HttpStatus responseStatus;
private HttpStatusCode responseStatus;
@Nullable
private String responseStatusReason;
@@ -296,7 +296,7 @@ public class HandlerMethod {
* @see ResponseStatus#code()
*/
@Nullable
protected HttpStatus getResponseStatus() {
protected HttpStatusCode getResponseStatus() {
return this.responseStatus;
}

View File

@@ -20,7 +20,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
@@ -62,7 +62,7 @@ public class ModelAndViewContainer {
private boolean redirectModelScenario = false;
@Nullable
private HttpStatus status;
private HttpStatusCode status;
private final Set<String> noBinding = new HashSet<>(4);
@@ -193,7 +193,7 @@ public class ModelAndViewContainer {
* {@code ModelAndView} used for view rendering purposes.
* @since 4.3
*/
public void setStatus(@Nullable HttpStatus status) {
public void setStatus(@Nullable HttpStatusCode status) {
this.status = status;
}
@@ -202,7 +202,7 @@ public class ModelAndViewContainer {
* @since 4.3
*/
@Nullable
public HttpStatus getStatus() {
public HttpStatusCode getStatus() {
return this.status;
}

View File

@@ -18,7 +18,7 @@ package org.springframework.web.server;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
import org.springframework.web.ErrorResponseException;
@@ -41,7 +41,7 @@ public class ResponseStatusException extends ErrorResponseException {
* Constructor with a response status.
* @param status the HTTP status (required)
*/
public ResponseStatusException(HttpStatus status) {
public ResponseStatusException(HttpStatusCode status) {
this(status, null);
}
@@ -51,21 +51,10 @@ public class ResponseStatusException extends ErrorResponseException {
* @param status the HTTP status (required)
* @param reason the associated reason (optional)
*/
public ResponseStatusException(HttpStatus status, @Nullable String reason) {
public ResponseStatusException(HttpStatusCode status, @Nullable String reason) {
this(status, reason, null);
}
/**
* Constructor with a response status and a reason to add to the exception
* message as explanation, as well as a nested exception.
* @param status the HTTP status (required)
* @param reason the associated reason (optional)
* @param cause a nested exception (optional)
*/
public ResponseStatusException(HttpStatus status, @Nullable String reason, @Nullable Throwable cause) {
this(status.value(), reason, cause);
}
/**
* Constructor with a response status and a reason to add to the exception
* message as explanation, as well as a nested exception.
@@ -75,7 +64,18 @@ public class ResponseStatusException extends ErrorResponseException {
* @since 5.3
*/
public ResponseStatusException(int rawStatusCode, @Nullable String reason, @Nullable Throwable cause) {
super(rawStatusCode, cause);
this(HttpStatusCode.valueOf(rawStatusCode), reason, cause);
}
/**
* Constructor with a response status and a reason to add to the exception
* message as explanation, as well as a nested exception.
* @param status the HTTP status (required)
* @param reason the associated reason (optional)
* @param cause a nested exception (optional)
*/
public ResponseStatusException(HttpStatusCode status, @Nullable String reason, @Nullable Throwable cause) {
super(status, cause);
this.reason = reason;
}
@@ -112,8 +112,7 @@ public class ResponseStatusException extends ErrorResponseException {
@Override
public String getMessage() {
HttpStatus code = HttpStatus.resolve(getRawStatusCode());
String msg = (code != null ? code : getRawStatusCode()) + (this.reason != null ? " \"" + this.reason + "\"" : "");
String msg = getStatusCode() + (this.reason != null ? " \"" + this.reason + "\"" : "");
return NestedExceptionUtils.buildMessage(msg, getCause());
}

View File

@@ -34,6 +34,7 @@ import org.springframework.core.codec.Hints;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageReader;
@@ -249,7 +250,7 @@ public class DefaultServerWebExchange implements ServerWebExchange {
@Override
public boolean checkNotModified(@Nullable String etag, Instant lastModified) {
HttpStatus status = getResponse().getStatusCode();
HttpStatusCode status = getResponse().getStatusCode();
if (this.notModified || (status != null && !HttpStatus.OK.equals(status))) {
return this.notModified;
}

View File

@@ -29,6 +29,7 @@ import org.springframework.core.NestedExceptionUtils;
import org.springframework.core.log.LogFormatUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.codec.LoggingCodecSupport;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.server.reactive.HttpHandler;
@@ -270,7 +271,7 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa
private void logResponse(ServerWebExchange exchange) {
LogFormatUtils.traceDebug(logger, traceOn -> {
HttpStatus status = exchange.getResponse().getStatusCode();
HttpStatusCode status = exchange.getResponse().getStatusCode();
return exchange.getLogPrefix() + "Completed " + (status != null ? status : "200 OK") +
(traceOn ? ", headers=" + formatHeaders(exchange.getResponse().getHeaders()) : "");
});

View File

@@ -21,7 +21,7 @@ import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Mono;
import org.springframework.core.log.LogFormatUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.lang.Nullable;
@@ -88,12 +88,13 @@ public class ResponseStatusExceptionHandler implements WebExceptionHandler {
return "Resolved [" + className + ": " + message + "] for HTTP " + request.getMethod() + " " + path;
}
@SuppressWarnings("deprecation")
private boolean updateResponse(ServerHttpResponse response, Throwable ex) {
boolean result = false;
HttpStatus httpStatus = determineStatus(ex);
int code = (httpStatus != null ? httpStatus.value() : determineRawStatusCode(ex));
HttpStatusCode statusCode = determineStatus(ex);
int code = (statusCode != null ? statusCode.value() : determineRawStatusCode(ex));
if (code != -1) {
if (response.setRawStatusCode(code)) {
if (response.setStatusCode(statusCode)) {
if (ex instanceof ResponseStatusException) {
((ResponseStatusException) ex).getHeaders().forEach((name, values) ->
values.forEach(value -> response.getHeaders().add(name, value)));
@@ -112,16 +113,18 @@ public class ResponseStatusExceptionHandler implements WebExceptionHandler {
/**
* Determine the HTTP status for the given exception.
* <p>As of 5.3 this method always returns {@code null} in which case
* {@link #determineRawStatusCode(Throwable)} is used instead.
* @param ex the exception to check
* @return the associated HTTP status, if any
* @deprecated as of 5.3 in favor of {@link #determineRawStatusCode(Throwable)}.
* @return the associated HTTP status code, or {@code null} if it can't be
* derived
*/
@Nullable
@Deprecated
protected HttpStatus determineStatus(Throwable ex) {
return null;
protected HttpStatusCode determineStatus(Throwable ex) {
if (ex instanceof ResponseStatusException responseStatusException) {
return responseStatusException.getStatusCode();
}
else {
return null;
}
}
/**
@@ -129,10 +132,12 @@ public class ResponseStatusExceptionHandler implements WebExceptionHandler {
* @param ex the exception to check
* @return the associated HTTP status code, or -1 if it can't be derived.
* @since 5.3
* @deprecated as of 6.0, in favor of {@link #determineStatus(Throwable)}
*/
@Deprecated
protected int determineRawStatusCode(Throwable ex) {
if (ex instanceof ResponseStatusException) {
return ((ResponseStatusException) ex).getRawStatusCode();
if (ex instanceof ResponseStatusException responseStatusException) {
return responseStatusException.getStatusCode().value();
}
return -1;
}