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:
@@ -30,7 +30,7 @@ import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -52,21 +52,18 @@ import org.springframework.web.reactive.function.BodyExtractor;
|
||||
public interface ClientResponse {
|
||||
|
||||
/**
|
||||
* 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} value.
|
||||
* @return the HTTP status as an HttpStatusCode value (never {@code null})
|
||||
*/
|
||||
HttpStatus statusCode();
|
||||
HttpStatusCode statusCode();
|
||||
|
||||
/**
|
||||
* Return the (potentially non-standard) status code of this response.
|
||||
* Return the raw status code of this response.
|
||||
* @return the HTTP status as an integer value
|
||||
* @since 5.1
|
||||
* @see #statusCode()
|
||||
* @see HttpStatus#resolve(int)
|
||||
* @deprecated as of 6.0, in favor of {@link #statusCode()}
|
||||
*/
|
||||
@Deprecated
|
||||
int rawStatusCode();
|
||||
|
||||
/**
|
||||
@@ -238,7 +235,7 @@ public interface ClientResponse {
|
||||
* @param statusCode the status code
|
||||
* @return the created builder
|
||||
*/
|
||||
static Builder create(HttpStatus statusCode) {
|
||||
static Builder create(HttpStatusCode statusCode) {
|
||||
return create(statusCode, ExchangeStrategies.withDefaults());
|
||||
}
|
||||
|
||||
@@ -248,7 +245,7 @@ public interface ClientResponse {
|
||||
* @param strategies the strategies
|
||||
* @return the created builder
|
||||
*/
|
||||
static Builder create(HttpStatus statusCode, ExchangeStrategies strategies) {
|
||||
static Builder create(HttpStatusCode statusCode, ExchangeStrategies strategies) {
|
||||
return new DefaultClientResponseBuilder(strategies).statusCode(statusCode);
|
||||
}
|
||||
|
||||
@@ -269,7 +266,7 @@ public interface ClientResponse {
|
||||
* @param messageReaders the message readers
|
||||
* @return the created builder
|
||||
*/
|
||||
static Builder create(HttpStatus statusCode, List<HttpMessageReader<?>> messageReaders) {
|
||||
static Builder create(HttpStatusCode statusCode, List<HttpMessageReader<?>> messageReaders) {
|
||||
return create(statusCode, new ExchangeStrategies() {
|
||||
@Override
|
||||
public List<HttpMessageReader<?>> messageReaders() {
|
||||
@@ -326,7 +323,7 @@ public interface ClientResponse {
|
||||
* @param statusCode the new status code
|
||||
* @return this builder
|
||||
*/
|
||||
Builder statusCode(HttpStatus statusCode);
|
||||
Builder statusCode(HttpStatusCode statusCode);
|
||||
|
||||
/**
|
||||
* Set the raw status code of the response.
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -106,11 +107,12 @@ class DefaultClientResponse implements ClientResponse {
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpStatus statusCode() {
|
||||
public HttpStatusCode statusCode() {
|
||||
return this.response.getStatusCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public int rawStatusCode() {
|
||||
return this.response.getRawStatusCode();
|
||||
}
|
||||
@@ -201,9 +203,8 @@ class DefaultClientResponse implements ClientResponse {
|
||||
.map(bodyBytes -> {
|
||||
HttpRequest request = this.requestSupplier.get();
|
||||
Charset charset = headers().contentType().map(MimeType::getCharset).orElse(null);
|
||||
int statusCode = rawStatusCode();
|
||||
HttpStatus httpStatus = HttpStatus.resolve(statusCode);
|
||||
if (httpStatus != null) {
|
||||
HttpStatusCode statusCode = statusCode();
|
||||
if (statusCode instanceof HttpStatus httpStatus) {
|
||||
return WebClientResponseException.create(
|
||||
statusCode,
|
||||
httpStatus.getReasonPhrase(),
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.http.client.reactive.ClientHttpResponse;
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -75,7 +76,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
|
||||
|
||||
private final ExchangeStrategies strategies;
|
||||
|
||||
private int statusCode = 200;
|
||||
private HttpStatusCode statusCode = HttpStatus.OK;
|
||||
|
||||
@Nullable
|
||||
private HttpHeaders headers;
|
||||
@@ -102,7 +103,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
|
||||
DefaultClientResponseBuilder(ClientResponse other, boolean mutate) {
|
||||
Assert.notNull(other, "ClientResponse must not be null");
|
||||
this.strategies = other.strategies();
|
||||
this.statusCode = other.rawStatusCode();
|
||||
this.statusCode = other.statusCode();
|
||||
if (mutate) {
|
||||
this.body = other.bodyToFlux(DataBuffer.class);
|
||||
}
|
||||
@@ -117,15 +118,15 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
|
||||
|
||||
|
||||
@Override
|
||||
public DefaultClientResponseBuilder statusCode(HttpStatus statusCode) {
|
||||
return rawStatusCode(statusCode.value());
|
||||
public DefaultClientResponseBuilder statusCode(HttpStatusCode statusCode) {
|
||||
Assert.notNull(statusCode, "StatusCode must not be null");
|
||||
this.statusCode = statusCode;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultClientResponseBuilder rawStatusCode(int statusCode) {
|
||||
Assert.isTrue(statusCode >= 100 && statusCode < 600, "StatusCode must be between 1xx and 5xx");
|
||||
this.statusCode = statusCode;
|
||||
return this;
|
||||
return statusCode(HttpStatusCode.valueOf(statusCode));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -224,7 +225,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
|
||||
|
||||
private static class BuiltClientHttpResponse implements ClientHttpResponse {
|
||||
|
||||
private final int statusCode;
|
||||
private final HttpStatusCode statusCode;
|
||||
|
||||
@Nullable
|
||||
private final HttpHeaders headers;
|
||||
@@ -238,7 +239,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
|
||||
private final ClientResponse originalResponse;
|
||||
|
||||
|
||||
BuiltClientHttpResponse(int statusCode, @Nullable HttpHeaders headers,
|
||||
BuiltClientHttpResponse(HttpStatusCode statusCode, @Nullable HttpHeaders headers,
|
||||
@Nullable MultiValueMap<String, ResponseCookie> cookies, Flux<DataBuffer> body,
|
||||
@Nullable ClientResponse originalResponse) {
|
||||
|
||||
@@ -256,13 +257,14 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpStatus getStatusCode() {
|
||||
return HttpStatus.valueOf(this.statusCode);
|
||||
public HttpStatusCode getStatusCode() {
|
||||
return this.statusCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public int getRawStatusCode() {
|
||||
return this.statusCode;
|
||||
return this.statusCode.value();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -39,7 +39,7 @@ import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.reactive.ClientHttpRequest;
|
||||
@@ -491,7 +491,7 @@ class DefaultWebClient implements WebClient {
|
||||
|
||||
private static class DefaultResponseSpec implements ResponseSpec {
|
||||
|
||||
private static final IntPredicate STATUS_CODE_ERROR = (value -> value >= 400);
|
||||
private static final Predicate<HttpStatusCode> STATUS_CODE_ERROR = HttpStatusCode::isError;
|
||||
|
||||
private static final StatusHandler DEFAULT_STATUS_HANDLER =
|
||||
new StatusHandler(STATUS_CODE_ERROR, ClientResponse::createException);
|
||||
@@ -511,28 +511,26 @@ class DefaultWebClient implements WebClient {
|
||||
|
||||
|
||||
@Override
|
||||
public ResponseSpec onStatus(Predicate<HttpStatus> statusPredicate,
|
||||
public ResponseSpec onStatus(Predicate<HttpStatusCode> statusCodePredicate,
|
||||
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
|
||||
|
||||
return onRawStatus(toIntPredicate(statusPredicate), exceptionFunction);
|
||||
}
|
||||
Assert.notNull(statusCodePredicate, "StatusCodePredicate must not be null");
|
||||
Assert.notNull(exceptionFunction, "Function must not be null");
|
||||
int index = this.statusHandlers.size() - 1; // Default handler always last
|
||||
this.statusHandlers.add(index, new StatusHandler(statusCodePredicate, exceptionFunction));
|
||||
return this;
|
||||
|
||||
private static IntPredicate toIntPredicate(Predicate<HttpStatus> predicate) {
|
||||
return value -> {
|
||||
HttpStatus status = HttpStatus.resolve(value);
|
||||
return (status != null && predicate.test(status));
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseSpec onRawStatus(IntPredicate statusCodePredicate,
|
||||
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
|
||||
|
||||
Assert.notNull(statusCodePredicate, "IntPredicate must not be null");
|
||||
Assert.notNull(exceptionFunction, "Function must not be null");
|
||||
int index = this.statusHandlers.size() - 1; // Default handler always last
|
||||
this.statusHandlers.add(index, new StatusHandler(statusCodePredicate, exceptionFunction));
|
||||
return this;
|
||||
return onStatus(toStatusCodePredicate(statusCodePredicate), exceptionFunction);
|
||||
}
|
||||
|
||||
private static Predicate<HttpStatusCode> toStatusCodePredicate(IntPredicate predicate) {
|
||||
return value -> predicate.test(value.value());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -635,7 +633,7 @@ class DefaultWebClient implements WebClient {
|
||||
ResponseEntity<Flux<T>> entity = new ResponseEntity<>(
|
||||
body.onErrorResume(WebClientUtils.WRAP_EXCEPTION_PREDICATE, exceptionWrappingFunction(response)),
|
||||
response.headers().asHttpHeaders(),
|
||||
response.rawStatusCode());
|
||||
response.statusCode());
|
||||
|
||||
Mono<ResponseEntity<Flux<T>>> result = applyStatusHandlers(response);
|
||||
return (result != null ? result.defaultIfEmpty(entity) : Mono.just(entity));
|
||||
@@ -647,7 +645,7 @@ class DefaultWebClient implements WebClient {
|
||||
|
||||
@Nullable
|
||||
private <T> Mono<T> applyStatusHandlers(ClientResponse response) {
|
||||
int statusCode = response.rawStatusCode();
|
||||
HttpStatusCode statusCode = response.statusCode();
|
||||
for (StatusHandler handler : this.statusHandlers) {
|
||||
if (handler.test(statusCode)) {
|
||||
Mono<? extends Throwable> exMono;
|
||||
@@ -667,7 +665,7 @@ class DefaultWebClient implements WebClient {
|
||||
return null;
|
||||
}
|
||||
|
||||
private <T> Mono<T> insertCheckpoint(Mono<T> result, int statusCode, HttpRequest request) {
|
||||
private <T> Mono<T> insertCheckpoint(Mono<T> result, HttpStatusCode statusCode, HttpRequest request) {
|
||||
HttpMethod httpMethod = request.getMethod();
|
||||
URI uri = request.getURI();
|
||||
String description = statusCode + " from " + httpMethod + " " + uri + " [DefaultWebClient]";
|
||||
@@ -677,18 +675,18 @@ class DefaultWebClient implements WebClient {
|
||||
|
||||
private static class StatusHandler {
|
||||
|
||||
private final IntPredicate predicate;
|
||||
private final Predicate<HttpStatusCode> predicate;
|
||||
|
||||
private final Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction;
|
||||
|
||||
public StatusHandler(IntPredicate predicate,
|
||||
public StatusHandler(Predicate<HttpStatusCode> predicate,
|
||||
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
|
||||
|
||||
this.predicate = predicate;
|
||||
this.exceptionFunction = exceptionFunction;
|
||||
}
|
||||
|
||||
public boolean test(int status) {
|
||||
public boolean test(HttpStatusCode status) {
|
||||
return this.predicate.test(status);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
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.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -66,12 +66,12 @@ public abstract class ExchangeFilterFunctions {
|
||||
|
||||
/**
|
||||
* Return a filter that generates an error signal when the given
|
||||
* {@link HttpStatus} predicate matches.
|
||||
* {@link HttpStatusCode} predicate matches.
|
||||
* @param statusPredicate the predicate to check the HTTP status with
|
||||
* @param exceptionFunction the function that to create the exception
|
||||
* @return the filter to generate an error signal
|
||||
*/
|
||||
public static ExchangeFilterFunction statusError(Predicate<HttpStatus> statusPredicate,
|
||||
public static ExchangeFilterFunction statusError(Predicate<HttpStatusCode> statusPredicate,
|
||||
Function<ClientResponse, ? extends Throwable> exceptionFunction) {
|
||||
|
||||
Assert.notNull(statusPredicate, "Predicate must not be null");
|
||||
|
||||
@@ -26,7 +26,6 @@ import org.springframework.core.log.LogFormatUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.client.reactive.ClientHttpConnector;
|
||||
import org.springframework.http.client.reactive.ClientHttpResponse;
|
||||
import org.springframework.http.codec.LoggingCodecSupport;
|
||||
@@ -125,12 +124,8 @@ public abstract class ExchangeFunctions {
|
||||
}
|
||||
|
||||
private void logResponse(ClientHttpResponse response, String logPrefix) {
|
||||
LogFormatUtils.traceDebug(logger, traceOn -> {
|
||||
int code = response.getRawStatusCode();
|
||||
HttpStatus status = HttpStatus.resolve(code);
|
||||
return logPrefix + "Response " + (status != null ? status : code) +
|
||||
(traceOn ? ", headers=" + formatHeaders(response.getHeaders()) : "");
|
||||
});
|
||||
LogFormatUtils.traceDebug(logger, traceOn -> logPrefix + "Response " + response.getStatusCode() +
|
||||
(traceOn ? ", headers=" + formatHeaders(response.getHeaders()) : ""));
|
||||
}
|
||||
|
||||
private String formatHeaders(HttpHeaders headers) {
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.nio.charset.Charset;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
@@ -57,4 +58,17 @@ public class UnknownHttpStatusCodeException extends WebClientResponseException {
|
||||
headers, responseBody, responseCharset, request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the {@code UnknownHttpStatusCodeException} with the given
|
||||
* parameters.
|
||||
* @since 6.0
|
||||
*/
|
||||
public UnknownHttpStatusCodeException(
|
||||
HttpStatusCode statusCode, HttpHeaders headers, byte[] responseBody, @Nullable Charset responseCharset,
|
||||
@Nullable HttpRequest request) {
|
||||
|
||||
super("Unknown status code [" + statusCode + "]", statusCode, "",
|
||||
headers, responseBody, responseCharset, request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.core.ReactiveAdapterRegistry;
|
||||
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.ResponseEntity;
|
||||
import org.springframework.http.client.reactive.ClientHttpConnector;
|
||||
@@ -767,7 +767,7 @@ public interface WebClient {
|
||||
* @return this builder
|
||||
* @see ClientResponse#createException()
|
||||
*/
|
||||
ResponseSpec onStatus(Predicate<HttpStatus> statusPredicate,
|
||||
ResponseSpec onStatus(Predicate<HttpStatusCode> statusPredicate,
|
||||
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction);
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
@@ -35,7 +36,7 @@ public class WebClientResponseException extends WebClientException {
|
||||
private static final long serialVersionUID = 4127543205414951611L;
|
||||
|
||||
|
||||
private final int statusCode;
|
||||
private final HttpStatusCode statusCode;
|
||||
|
||||
private final String statusText;
|
||||
|
||||
@@ -68,11 +69,21 @@ public class WebClientResponseException extends WebClientException {
|
||||
@Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset charset,
|
||||
@Nullable HttpRequest request) {
|
||||
|
||||
this(initMessage(status, reasonPhrase, request), status, reasonPhrase, headers, body, charset, request);
|
||||
this(HttpStatusCode.valueOf(status), reasonPhrase, headers, body, charset, request);
|
||||
}
|
||||
|
||||
private static String initMessage(int status, String reasonPhrase, @Nullable HttpRequest request) {
|
||||
return status + " " + reasonPhrase +
|
||||
/**
|
||||
* Constructor with response data only, and a default message.
|
||||
* @since 6.0
|
||||
*/
|
||||
public WebClientResponseException(HttpStatusCode statusCode, String reasonPhrase,
|
||||
@Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset charset,
|
||||
@Nullable HttpRequest request) {
|
||||
this(initMessage(statusCode, reasonPhrase, request), statusCode, reasonPhrase, headers, body, charset, request);
|
||||
}
|
||||
|
||||
private static String initMessage(HttpStatusCode status, String reasonPhrase, @Nullable HttpRequest request) {
|
||||
return status.value() + " " + reasonPhrase +
|
||||
(request != null ? " from " + request.getMethod() + " " + request.getURI() : "");
|
||||
}
|
||||
|
||||
@@ -91,6 +102,17 @@ public class WebClientResponseException extends WebClientException {
|
||||
public WebClientResponseException(String message, int statusCode, String statusText,
|
||||
@Nullable HttpHeaders headers, @Nullable byte[] responseBody, @Nullable Charset charset,
|
||||
@Nullable HttpRequest request) {
|
||||
this(message, HttpStatusCode.valueOf(statusCode), statusText, headers, responseBody, charset, request);
|
||||
|
||||
|
||||
}
|
||||
/**
|
||||
* Constructor with a prepared message.
|
||||
* @since 6.0
|
||||
*/
|
||||
public WebClientResponseException(String message, HttpStatusCode statusCode, String statusText,
|
||||
@Nullable HttpHeaders headers, @Nullable byte[] responseBody, @Nullable Charset charset,
|
||||
@Nullable HttpRequest request) {
|
||||
|
||||
super(message);
|
||||
|
||||
@@ -107,15 +129,17 @@ public class WebClientResponseException extends WebClientException {
|
||||
* Return the HTTP status code value.
|
||||
* @throws IllegalArgumentException in case of an unknown HTTP status code
|
||||
*/
|
||||
public HttpStatus getStatusCode() {
|
||||
return HttpStatus.valueOf(this.statusCode);
|
||||
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.statusCode;
|
||||
return this.statusCode.value();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,9 +212,18 @@ public class WebClientResponseException extends WebClientException {
|
||||
public static WebClientResponseException create(
|
||||
int statusCode, String statusText, HttpHeaders headers, byte[] body,
|
||||
@Nullable Charset charset, @Nullable HttpRequest request) {
|
||||
return create(HttpStatusCode.valueOf(statusCode), statusText, headers, body, charset, request);
|
||||
}
|
||||
|
||||
HttpStatus httpStatus = HttpStatus.resolve(statusCode);
|
||||
if (httpStatus != null) {
|
||||
/**
|
||||
* Create {@code WebClientResponseException} or an HTTP status specific subclass.
|
||||
* @since 6.0
|
||||
*/
|
||||
public static WebClientResponseException create(
|
||||
HttpStatusCode statusCode, String statusText, HttpHeaders headers, byte[] body,
|
||||
@Nullable Charset charset, @Nullable HttpRequest request) {
|
||||
|
||||
if (statusCode instanceof HttpStatus httpStatus) {
|
||||
switch (httpStatus) {
|
||||
case BAD_REQUEST:
|
||||
return new WebClientResponseException.BadRequest(statusText, headers, body, charset, request);
|
||||
|
||||
@@ -53,7 +53,7 @@ abstract class WebClientUtils {
|
||||
new ResponseEntity<>(
|
||||
body != VALUE_NONE ? (T) body : null,
|
||||
response.headers().asHttpHeaders(),
|
||||
response.rawStatusCode()));
|
||||
response.statusCode()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,7 +61,7 @@ abstract class WebClientUtils {
|
||||
*/
|
||||
public static <T> Mono<ResponseEntity<List<T>>> mapToEntityList(ClientResponse response, Publisher<T> body) {
|
||||
return Flux.from(body).collectList().map(list ->
|
||||
new ResponseEntity<>(list, response.headers().asHttpHeaders(), response.rawStatusCode()));
|
||||
new ResponseEntity<>(list, response.headers().asHttpHeaders(), response.statusCode()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
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.http.ResponseEntity;
|
||||
@@ -74,11 +74,12 @@ public class ClientResponseWrapper implements ClientResponse {
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpStatus statusCode() {
|
||||
public HttpStatusCode statusCode() {
|
||||
return this.delegate.statusCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public int rawStatusCode() {
|
||||
return this.delegate.rawStatusCode();
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.http.CacheControl;
|
||||
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.ResponseCookie;
|
||||
import org.springframework.http.codec.HttpMessageWriter;
|
||||
@@ -60,7 +61,7 @@ class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T> {
|
||||
|
||||
private final BodyInserter<T, ? super ServerHttpResponse> inserter;
|
||||
|
||||
private int status = HttpStatus.OK.value();
|
||||
private HttpStatusCode status = HttpStatus.OK;
|
||||
|
||||
private final HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
@@ -76,16 +77,15 @@ class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T> {
|
||||
|
||||
|
||||
@Override
|
||||
public EntityResponse.Builder<T> status(HttpStatus status) {
|
||||
Assert.notNull(status, "HttpStatus must not be null");
|
||||
this.status = status.value();
|
||||
public EntityResponse.Builder<T> status(HttpStatusCode status) {
|
||||
Assert.notNull(status, "HttpStatusCode must not be null");
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityResponse.Builder<T> status(int status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
return status(HttpStatusCode.valueOf(status));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -209,7 +209,7 @@ class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T> {
|
||||
private final BodyInserter<T, ? super ServerHttpResponse> inserter;
|
||||
|
||||
|
||||
public DefaultEntityResponse(int statusCode, HttpHeaders headers,
|
||||
public DefaultEntityResponse(HttpStatusCode statusCode, HttpHeaders headers,
|
||||
MultiValueMap<String, ResponseCookie> cookies, T entity,
|
||||
BodyInserter<T, ? super ServerHttpResponse> inserter, Map<String, Object> hints) {
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.core.Conventions;
|
||||
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;
|
||||
@@ -53,7 +54,7 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder
|
||||
|
||||
private final String name;
|
||||
|
||||
private int status = HttpStatus.OK.value();
|
||||
private HttpStatusCode status = HttpStatus.OK;
|
||||
|
||||
private final HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
@@ -65,8 +66,7 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder
|
||||
public DefaultRenderingResponseBuilder(RenderingResponse other) {
|
||||
Assert.notNull(other, "RenderingResponse must not be null");
|
||||
this.name = other.name();
|
||||
this.status = (other instanceof DefaultRenderingResponse ?
|
||||
((DefaultRenderingResponse) other).statusCode : other.statusCode().value());
|
||||
this.status = other.statusCode();
|
||||
this.headers.putAll(other.headers());
|
||||
this.model.putAll(other.model());
|
||||
}
|
||||
@@ -78,16 +78,15 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder
|
||||
|
||||
|
||||
@Override
|
||||
public RenderingResponse.Builder status(HttpStatus status) {
|
||||
Assert.notNull(status, "HttpStatus must not be null");
|
||||
this.status = status.value();
|
||||
public RenderingResponse.Builder status(HttpStatusCode status) {
|
||||
Assert.notNull(status, "HttpStatusCode must not be null");
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RenderingResponse.Builder status(int status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
return status(HttpStatusCode.valueOf(status));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -165,7 +164,7 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder
|
||||
|
||||
private final Map<String, Object> model;
|
||||
|
||||
public DefaultRenderingResponse(int statusCode, HttpHeaders headers,
|
||||
public DefaultRenderingResponse(HttpStatusCode statusCode, HttpHeaders headers,
|
||||
MultiValueMap<String, ResponseCookie> cookies, String name, Map<String, Object> model) {
|
||||
|
||||
super(statusCode, headers, cookies, Collections.emptyMap());
|
||||
|
||||
@@ -41,6 +41,8 @@ import org.springframework.http.HttpCookie;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRange;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.HttpMessageReader;
|
||||
import org.springframework.http.codec.multipart.Part;
|
||||
@@ -98,8 +100,8 @@ class DefaultServerRequest implements ServerRequest {
|
||||
}
|
||||
|
||||
if (exchange.checkNotModified(etag, lastModified)) {
|
||||
Integer statusCode = exchange.getResponse().getRawStatusCode();
|
||||
return ServerResponse.status(statusCode != null ? statusCode : 200)
|
||||
HttpStatusCode statusCode = exchange.getResponse().getStatusCode();
|
||||
return ServerResponse.status(statusCode != null ? statusCode : HttpStatus.OK)
|
||||
.headers(headers -> headers.addAll(exchange.getResponse().getHeaders()))
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ import org.springframework.core.codec.Hints;
|
||||
import org.springframework.http.CacheControl;
|
||||
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.ReactiveHttpOutputMessage;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
@@ -64,7 +64,7 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
*/
|
||||
class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||
|
||||
private final int statusCode;
|
||||
private final HttpStatusCode statusCode;
|
||||
|
||||
private final HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
@@ -77,22 +77,15 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||
Assert.notNull(other, "ServerResponse must not be null");
|
||||
this.headers.addAll(other.headers());
|
||||
this.cookies.addAll(other.cookies());
|
||||
this.statusCode = other.statusCode();
|
||||
if (other instanceof AbstractServerResponse abstractOther) {
|
||||
this.statusCode = abstractOther.statusCode;
|
||||
this.hints.putAll(abstractOther.hints);
|
||||
}
|
||||
else {
|
||||
this.statusCode = other.statusCode().value();
|
||||
}
|
||||
}
|
||||
|
||||
public DefaultServerResponseBuilder(HttpStatus status) {
|
||||
Assert.notNull(status, "HttpStatus must not be null");
|
||||
this.statusCode = status.value();
|
||||
}
|
||||
|
||||
public DefaultServerResponseBuilder(int statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
public DefaultServerResponseBuilder(HttpStatusCode status) {
|
||||
Assert.notNull(status, "HttpStatusCode must not be null");
|
||||
this.statusCode = status;
|
||||
}
|
||||
|
||||
|
||||
@@ -298,7 +291,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||
|
||||
private static final Set<HttpMethod> SAFE_METHODS = Set.of(HttpMethod.GET, HttpMethod.HEAD);
|
||||
|
||||
final int statusCode;
|
||||
private final HttpStatusCode statusCode;
|
||||
|
||||
private final HttpHeaders headers;
|
||||
|
||||
@@ -308,7 +301,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||
|
||||
|
||||
protected AbstractServerResponse(
|
||||
int statusCode, HttpHeaders headers, MultiValueMap<String, ResponseCookie> cookies,
|
||||
HttpStatusCode statusCode, HttpHeaders headers, MultiValueMap<String, ResponseCookie> cookies,
|
||||
Map<String, Object> hints) {
|
||||
|
||||
this.statusCode = statusCode;
|
||||
@@ -318,13 +311,14 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final HttpStatus statusCode() {
|
||||
return HttpStatus.valueOf(this.statusCode);
|
||||
public final HttpStatusCode statusCode() {
|
||||
return this.statusCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public int rawStatusCode() {
|
||||
return this.statusCode;
|
||||
return this.statusCode.value();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -351,7 +345,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||
}
|
||||
|
||||
private void writeStatusAndHeaders(ServerHttpResponse response) {
|
||||
response.setRawStatusCode(this.statusCode);
|
||||
response.setStatusCode(this.statusCode);
|
||||
copy(this.headers, response.getHeaders());
|
||||
copy(this.cookies, response.getCookies());
|
||||
}
|
||||
@@ -370,7 +364,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||
|
||||
private final BiFunction<ServerWebExchange, Context, Mono<Void>> writeFunction;
|
||||
|
||||
public WriterFunctionResponse(int statusCode, HttpHeaders headers,
|
||||
public WriterFunctionResponse(HttpStatusCode statusCode, HttpHeaders headers,
|
||||
MultiValueMap<String, ResponseCookie> cookies,
|
||||
BiFunction<ServerWebExchange, Context, Mono<Void>> writeFunction) {
|
||||
|
||||
@@ -391,7 +385,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||
private final BodyInserter<T, ? super ServerHttpResponse> inserter;
|
||||
|
||||
|
||||
public BodyInserterResponse(int statusCode, HttpHeaders headers,
|
||||
public BodyInserterResponse(HttpStatusCode statusCode, HttpHeaders headers,
|
||||
MultiValueMap<String, ResponseCookie> cookies,
|
||||
BodyInserter<T, ? super ServerHttpResponse> body, Map<String, Object> hints) {
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.CacheControl;
|
||||
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.ResponseCookie;
|
||||
import org.springframework.http.codec.json.Jackson2CodecSupport;
|
||||
@@ -154,7 +154,7 @@ public interface EntityResponse<T> extends ServerResponse {
|
||||
* @param status the response status
|
||||
* @return this builder
|
||||
*/
|
||||
Builder<T> status(HttpStatus status);
|
||||
Builder<T> status(HttpStatusCode status);
|
||||
|
||||
/**
|
||||
* Set the HTTP status.
|
||||
|
||||
@@ -23,7 +23,7 @@ import java.util.function.Consumer;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
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.MultiValueMap;
|
||||
@@ -134,7 +134,7 @@ public interface RenderingResponse extends ServerResponse {
|
||||
* @param status the response status
|
||||
* @return this builder
|
||||
*/
|
||||
Builder status(HttpStatus status);
|
||||
Builder status(HttpStatusCode status);
|
||||
|
||||
/**
|
||||
* Set the HTTP status.
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.http.CacheControl;
|
||||
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.ResponseCookie;
|
||||
import org.springframework.http.codec.HttpMessageWriter;
|
||||
@@ -60,19 +61,17 @@ public interface ServerResponse {
|
||||
|
||||
/**
|
||||
* Return the status code of this response.
|
||||
* @return the status as an HttpStatus enum value
|
||||
* @throws IllegalArgumentException in case of an unknown HTTP status code
|
||||
* @see HttpStatus#valueOf(int)
|
||||
* @return the status as an HttpStatusCode value
|
||||
*/
|
||||
HttpStatus statusCode();
|
||||
HttpStatusCode statusCode();
|
||||
|
||||
/**
|
||||
* Return the (potentially non-standard) status code of this response.
|
||||
* Return the status code of this response as integer.
|
||||
* @return the status as an integer
|
||||
* @since 5.2
|
||||
* @see #statusCode()
|
||||
* @see HttpStatus#resolve(int)
|
||||
* @deprecated as of 6.0, in favor of {@link #statusCode()}
|
||||
*/
|
||||
@Deprecated
|
||||
int rawStatusCode();
|
||||
|
||||
/**
|
||||
@@ -110,7 +109,7 @@ public interface ServerResponse {
|
||||
* @param status the response status
|
||||
* @return the created builder
|
||||
*/
|
||||
static BodyBuilder status(HttpStatus status) {
|
||||
static BodyBuilder status(HttpStatusCode status) {
|
||||
return new DefaultServerResponseBuilder(status);
|
||||
}
|
||||
|
||||
@@ -121,7 +120,7 @@ public interface ServerResponse {
|
||||
* @since 5.0.3
|
||||
*/
|
||||
static BodyBuilder status(int status) {
|
||||
return new DefaultServerResponseBuilder(status);
|
||||
return new DefaultServerResponseBuilder(HttpStatusCode.valueOf(status));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.web.reactive.handler;
|
||||
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.server.handler.ResponseStatusExceptionHandler;
|
||||
|
||||
@@ -37,15 +38,15 @@ import org.springframework.web.server.handler.ResponseStatusExceptionHandler;
|
||||
public class WebFluxResponseStatusExceptionHandler extends ResponseStatusExceptionHandler {
|
||||
|
||||
@Override
|
||||
protected int determineRawStatusCode(Throwable ex) {
|
||||
int status = super.determineRawStatusCode(ex);
|
||||
if (status == -1) {
|
||||
protected HttpStatusCode determineStatus(Throwable ex) {
|
||||
HttpStatusCode statusCode = super.determineStatus(ex);
|
||||
if (statusCode == null) {
|
||||
ResponseStatus ann = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class);
|
||||
if (ann != null) {
|
||||
status = ann.code().value();
|
||||
statusCode = ann.code();
|
||||
}
|
||||
}
|
||||
return status;
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ParameterNameDiscoverer;
|
||||
import org.springframework.core.ReactiveAdapter;
|
||||
import org.springframework.core.ReactiveAdapterRegistry;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
@@ -157,7 +157,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
||||
return Mono.error(new IllegalStateException(formatInvokeError("Invocation failure", args), ex));
|
||||
}
|
||||
|
||||
HttpStatus status = getResponseStatus();
|
||||
HttpStatusCode status = getResponseStatus();
|
||||
if (status != null) {
|
||||
exchange.getResponse().setStatusCode(status);
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ import org.springframework.core.ReactiveAdapter;
|
||||
import org.springframework.core.ReactiveAdapterRegistry;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.codec.Hints;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.HttpMessageWriter;
|
||||
import org.springframework.http.converter.HttpMessageNotWritableException;
|
||||
@@ -152,7 +152,7 @@ public abstract class AbstractMessageWriterResultHandler extends HandlerResultHa
|
||||
bestMediaType = selectMediaType(exchange, () -> getMediaTypesFor(elementType));
|
||||
}
|
||||
catch (NotAcceptableStatusException ex) {
|
||||
HttpStatus statusCode = exchange.getResponse().getStatusCode();
|
||||
HttpStatusCode statusCode = exchange.getResponse().getStatusCode();
|
||||
if (statusCode != null && statusCode.isError()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Ignoring error response content (if any). " + ex.getReason());
|
||||
|
||||
@@ -141,7 +141,7 @@ public class ResponseEntityResultHandler extends AbstractMessageWriterResultHand
|
||||
httpEntity = (HttpEntity<?>) returnValue;
|
||||
}
|
||||
else if (returnValue instanceof ErrorResponse response) {
|
||||
httpEntity = new ResponseEntity<>(response.getBody(), response.getHeaders(), response.getRawStatusCode());
|
||||
httpEntity = new ResponseEntity<>(response.getBody(), response.getHeaders(), response.getStatusCode());
|
||||
}
|
||||
else if (returnValue instanceof ProblemDetail detail) {
|
||||
httpEntity = new ResponseEntity<>(returnValue, HttpHeaders.EMPTY, detail.getStatus());
|
||||
@@ -161,9 +161,8 @@ public class ResponseEntityResultHandler extends AbstractMessageWriterResultHand
|
||||
}
|
||||
}
|
||||
|
||||
if (httpEntity instanceof ResponseEntity) {
|
||||
exchange.getResponse().setRawStatusCode(
|
||||
((ResponseEntity<?>) httpEntity).getStatusCodeValue());
|
||||
if (httpEntity instanceof ResponseEntity<?> responseEntity) {
|
||||
exchange.getResponse().setStatusCode(responseEntity.getStatusCode());
|
||||
}
|
||||
|
||||
HttpHeaders entityHeaders = httpEntity.getHeaders();
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.ui.Model;
|
||||
|
||||
@@ -40,12 +40,12 @@ class DefaultRendering implements Rendering {
|
||||
private final Map<String, Object> model;
|
||||
|
||||
@Nullable
|
||||
private final HttpStatus status;
|
||||
private final HttpStatusCode status;
|
||||
|
||||
private final HttpHeaders headers;
|
||||
|
||||
|
||||
DefaultRendering(Object view, @Nullable Model model, @Nullable HttpStatus status, @Nullable HttpHeaders headers) {
|
||||
DefaultRendering(Object view, @Nullable Model model, @Nullable HttpStatusCode status, @Nullable HttpHeaders headers) {
|
||||
this.view = view;
|
||||
this.model = (model != null ? model.asMap() : Collections.emptyMap());
|
||||
this.status = status;
|
||||
@@ -66,7 +66,7 @@ class DefaultRendering implements Rendering {
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public HttpStatus status() {
|
||||
public HttpStatusCode status() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.ui.ExtendedModelMap;
|
||||
import org.springframework.ui.Model;
|
||||
@@ -40,7 +40,7 @@ class DefaultRenderingBuilder implements Rendering.RedirectBuilder {
|
||||
private Model model;
|
||||
|
||||
@Nullable
|
||||
private HttpStatus status;
|
||||
private HttpStatusCode status;
|
||||
|
||||
@Nullable
|
||||
private HttpHeaders headers;
|
||||
@@ -83,7 +83,7 @@ class DefaultRenderingBuilder implements Rendering.RedirectBuilder {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultRenderingBuilder status(HttpStatus status) {
|
||||
public DefaultRenderingBuilder status(HttpStatusCode status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.regex.Pattern;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
@@ -56,7 +57,7 @@ public class RedirectView extends AbstractUrlBasedView {
|
||||
private static final Pattern URI_TEMPLATE_VARIABLE_PATTERN = Pattern.compile("\\{([^/]+?)\\}");
|
||||
|
||||
|
||||
private HttpStatus statusCode = HttpStatus.SEE_OTHER;
|
||||
private HttpStatusCode statusCode = HttpStatus.SEE_OTHER;
|
||||
|
||||
private boolean contextRelative = true;
|
||||
|
||||
@@ -85,7 +86,7 @@ public class RedirectView extends AbstractUrlBasedView {
|
||||
* redirect status code such as {@link HttpStatus#TEMPORARY_REDIRECT} or
|
||||
* {@link HttpStatus#PERMANENT_REDIRECT}.
|
||||
*/
|
||||
public RedirectView(String redirectUrl, HttpStatus statusCode) {
|
||||
public RedirectView(String redirectUrl, HttpStatusCode statusCode) {
|
||||
super(redirectUrl);
|
||||
setStatusCode(statusCode);
|
||||
}
|
||||
@@ -96,7 +97,7 @@ public class RedirectView extends AbstractUrlBasedView {
|
||||
* {@link HttpStatus#TEMPORARY_REDIRECT} or
|
||||
* {@link HttpStatus#PERMANENT_REDIRECT}.
|
||||
*/
|
||||
public void setStatusCode(HttpStatus statusCode) {
|
||||
public void setStatusCode(HttpStatusCode statusCode) {
|
||||
Assert.isTrue(statusCode.is3xxRedirection(), "Not a redirect status code");
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
@@ -104,7 +105,7 @@ public class RedirectView extends AbstractUrlBasedView {
|
||||
/**
|
||||
* Get the redirect status code to use.
|
||||
*/
|
||||
public HttpStatus getStatusCode() {
|
||||
public HttpStatusCode getStatusCode() {
|
||||
return this.statusCode;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.ui.Model;
|
||||
|
||||
@@ -58,7 +58,7 @@ public interface Rendering {
|
||||
* Return the HTTP status to set the response to.
|
||||
*/
|
||||
@Nullable
|
||||
HttpStatus status();
|
||||
HttpStatusCode status();
|
||||
|
||||
/**
|
||||
* Return headers to add to the response.
|
||||
@@ -121,7 +121,7 @@ public interface Rendering {
|
||||
/**
|
||||
* Specify the status to use for the response.
|
||||
*/
|
||||
B status(HttpStatus status);
|
||||
B status(HttpStatusCode status);
|
||||
|
||||
/**
|
||||
* Specify a header to add to the response.
|
||||
|
||||
@@ -37,7 +37,7 @@ import org.springframework.core.ReactiveAdapter;
|
||||
import org.springframework.core.ReactiveAdapterRegistry;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.ui.Model;
|
||||
@@ -214,7 +214,7 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport imp
|
||||
}
|
||||
else if (Rendering.class.isAssignableFrom(clazz)) {
|
||||
Rendering render = (Rendering) returnValue;
|
||||
HttpStatus status = render.status();
|
||||
HttpStatusCode status = render.status();
|
||||
if (status != null) {
|
||||
exchange.getResponse().setStatusCode(status);
|
||||
}
|
||||
@@ -325,7 +325,7 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport imp
|
||||
bestMediaType = selectMediaType(exchange, () -> mediaTypes);
|
||||
}
|
||||
catch (NotAcceptableStatusException ex) {
|
||||
HttpStatus statusCode = exchange.getResponse().getStatusCode();
|
||||
HttpStatusCode statusCode = exchange.getResponse().getStatusCode();
|
||||
if (statusCode != null && statusCode.isError()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Ignoring error response content (if any). " + ex.getReason());
|
||||
|
||||
@@ -21,7 +21,7 @@ import kotlinx.coroutines.reactive.awaitSingle
|
||||
import kotlinx.coroutines.reactor.mono
|
||||
import org.springframework.core.io.Resource
|
||||
import org.springframework.http.HttpMethod
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.HttpStatusCode
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions.nest
|
||||
import java.net.URI
|
||||
@@ -660,7 +660,7 @@ class CoRouterFunctionDsl internal constructor (private val init: (CoRouterFunct
|
||||
/**
|
||||
* @see ServerResponse.status
|
||||
*/
|
||||
fun status(status: HttpStatus) = ServerResponse.status(status)
|
||||
fun status(status: HttpStatusCode) = ServerResponse.status(status)
|
||||
|
||||
/**
|
||||
* @see ServerResponse.status
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.web.reactive.function.server
|
||||
import org.springframework.core.io.Resource
|
||||
import org.springframework.http.HttpMethod
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.HttpStatusCode
|
||||
import org.springframework.http.MediaType
|
||||
import reactor.core.publisher.Mono
|
||||
import java.net.URI
|
||||
@@ -725,7 +726,7 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs
|
||||
* @return the created builder
|
||||
* @since 5.1
|
||||
*/
|
||||
fun status(status: HttpStatus): ServerResponse.BodyBuilder =
|
||||
fun status(status: HttpStatusCode): ServerResponse.BodyBuilder =
|
||||
ServerResponse.status(status)
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,12 +28,12 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest;
|
||||
import org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -106,6 +106,6 @@ public class DefaultClientResponseBuilderTests {
|
||||
ClientResponse result = other.mutate().build();
|
||||
|
||||
assertThat(result.rawStatusCode()).isEqualTo(499);
|
||||
assertThatIllegalArgumentException().isThrownBy(result::statusCode);
|
||||
assertThat(result.statusCode()).isEqualTo(HttpStatusCode.valueOf(499));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpRange;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -49,7 +50,6 @@ import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -233,8 +233,7 @@ public class DefaultClientResponseTests {
|
||||
|
||||
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
|
||||
given(mockResponse.getHeaders()).willReturn(httpHeaders);
|
||||
given(mockResponse.getStatusCode()).willThrow(new IllegalArgumentException("999"));
|
||||
given(mockResponse.getRawStatusCode()).willReturn(999);
|
||||
given(mockResponse.getStatusCode()).willReturn(HttpStatusCode.valueOf(999));
|
||||
given(mockResponse.getBody()).willReturn(body);
|
||||
|
||||
List<HttpMessageReader<?>> messageReaders = Collections
|
||||
@@ -243,8 +242,7 @@ public class DefaultClientResponseTests {
|
||||
|
||||
ResponseEntity<String> result = defaultClientResponse.toEntity(String.class).block();
|
||||
assertThat(result.getBody()).isEqualTo("foo");
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
result::getStatusCode);
|
||||
assertThat(result.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(999));
|
||||
assertThat(result.getStatusCodeValue()).isEqualTo(999);
|
||||
assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
|
||||
}
|
||||
@@ -295,8 +293,7 @@ public class DefaultClientResponseTests {
|
||||
|
||||
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
|
||||
given(mockResponse.getHeaders()).willReturn(httpHeaders);
|
||||
given(mockResponse.getStatusCode()).willThrow(new IllegalArgumentException("999"));
|
||||
given(mockResponse.getRawStatusCode()).willReturn(999);
|
||||
given(mockResponse.getStatusCode()).willReturn(HttpStatusCode.valueOf(999));
|
||||
given(mockResponse.getBody()).willReturn(body);
|
||||
|
||||
List<HttpMessageReader<?>> messageReaders = Collections
|
||||
@@ -305,8 +302,7 @@ public class DefaultClientResponseTests {
|
||||
|
||||
ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block();
|
||||
assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
result::getStatusCode);
|
||||
assertThat(result.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(999));
|
||||
assertThat(result.getStatusCodeValue()).isEqualTo(999);
|
||||
assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ import org.springframework.core.NamedThreadLocal;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.ClientCodecConfigurer;
|
||||
import org.springframework.web.reactive.function.BodyExtractors;
|
||||
@@ -73,6 +74,7 @@ public class DefaultWebClientTests {
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
ClientResponse mockResponse = mock(ClientResponse.class);
|
||||
when(mockResponse.statusCode()).thenReturn(HttpStatus.OK);
|
||||
when(mockResponse.bodyToMono(Void.class)).thenReturn(Mono.empty());
|
||||
given(this.exchangeFunction.exchange(this.captor.capture())).willReturn(Mono.just(mockResponse));
|
||||
this.builder = WebClient.builder().baseUrl("/base").exchangeFunction(this.exchangeFunction);
|
||||
@@ -414,8 +416,8 @@ public class DefaultWebClientTests {
|
||||
Mono<Void> result = this.builder.build().get()
|
||||
.uri("/path")
|
||||
.retrieve()
|
||||
.onStatus(HttpStatus::is4xxClientError, resp -> Mono.error(new IllegalStateException("1")))
|
||||
.onStatus(HttpStatus::is4xxClientError, resp -> Mono.error(new IllegalStateException("2")))
|
||||
.onStatus(HttpStatusCode::is4xxClientError, resp -> Mono.error(new IllegalStateException("1")))
|
||||
.onStatus(HttpStatusCode::is4xxClientError, resp -> Mono.error(new IllegalStateException("2")))
|
||||
.bodyToMono(Void.class);
|
||||
|
||||
StepVerifier.create(result).expectErrorMessage("1").verify();
|
||||
@@ -428,8 +430,8 @@ public class DefaultWebClientTests {
|
||||
ClientResponse response = ClientResponse.create(HttpStatus.BAD_REQUEST).build();
|
||||
given(exchangeFunction.exchange(any())).willReturn(Mono.just(response));
|
||||
|
||||
Predicate<HttpStatus> predicate1 = mock(Predicate.class);
|
||||
Predicate<HttpStatus> predicate2 = mock(Predicate.class);
|
||||
Predicate<HttpStatusCode> predicate1 = mock(Predicate.class);
|
||||
Predicate<HttpStatusCode> predicate2 = mock(Predicate.class);
|
||||
|
||||
given(predicate1.test(HttpStatus.BAD_REQUEST)).willReturn(false);
|
||||
given(predicate2.test(HttpStatus.BAD_REQUEST)).willReturn(false);
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.web.reactive.function.BodyExtractors;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
@@ -170,7 +171,7 @@ public class ExchangeFilterFunctionsTests {
|
||||
ExchangeFunction exchange = r -> Mono.just(response);
|
||||
|
||||
ExchangeFilterFunction errorHandler = ExchangeFilterFunctions.statusError(
|
||||
HttpStatus::is4xxClientError, r -> new MyException());
|
||||
HttpStatusCode::is4xxClientError, r -> new MyException());
|
||||
|
||||
Mono<ClientResponse> result = errorHandler.filter(request, exchange);
|
||||
|
||||
@@ -186,7 +187,7 @@ public class ExchangeFilterFunctionsTests {
|
||||
given(response.statusCode()).willReturn(HttpStatus.NOT_FOUND);
|
||||
|
||||
Mono<ClientResponse> result = ExchangeFilterFunctions
|
||||
.statusError(HttpStatus::is5xxServerError, req -> new MyException())
|
||||
.statusError(HttpStatusCode::is5xxServerError, req -> new MyException())
|
||||
.filter(request, req -> Mono.just(response));
|
||||
|
||||
StepVerifier.create(result)
|
||||
|
||||
@@ -64,6 +64,7 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -741,7 +742,7 @@ class WebClientIntegrationTests {
|
||||
Mono<String> result = this.webClient.get()
|
||||
.uri("/greeting")
|
||||
.retrieve()
|
||||
.onStatus(HttpStatus::is5xxServerError, response -> Mono.just(new MyException("500 error!")))
|
||||
.onStatus(HttpStatusCode::is5xxServerError, response -> Mono.just(new MyException("500 error!")))
|
||||
.bodyToMono(String.class);
|
||||
|
||||
StepVerifier.create(result)
|
||||
@@ -765,7 +766,7 @@ class WebClientIntegrationTests {
|
||||
Mono<String> result = this.webClient.get()
|
||||
.uri("/greeting")
|
||||
.retrieve()
|
||||
.onStatus(HttpStatus::is5xxServerError, response -> Mono.just(new MyException("500 error!")))
|
||||
.onStatus(HttpStatusCode::is5xxServerError, response -> Mono.just(new MyException("500 error!")))
|
||||
.bodyToMono(new ParameterizedTypeReference<String>() {});
|
||||
|
||||
StepVerifier.create(result)
|
||||
@@ -792,7 +793,7 @@ class WebClientIntegrationTests {
|
||||
Mono<String> result = this.webClient.get()
|
||||
.uri("/json")
|
||||
.retrieve()
|
||||
.onStatus(HttpStatus::isError,
|
||||
.onStatus(HttpStatusCode::isError,
|
||||
response -> response.bodyToMono(Pojo.class)
|
||||
.flatMap(pojo -> Mono.error(new MyException(pojo.getFoo())))
|
||||
)
|
||||
@@ -841,7 +842,7 @@ class WebClientIntegrationTests {
|
||||
Mono<String> result = this.webClient.get()
|
||||
.uri("/greeting")
|
||||
.retrieve()
|
||||
.onStatus(HttpStatus::is5xxServerError, response -> Mono.empty())
|
||||
.onStatus(HttpStatusCode::is5xxServerError, response -> Mono.empty())
|
||||
.bodyToMono(String.class);
|
||||
|
||||
StepVerifier.create(result)
|
||||
@@ -865,7 +866,7 @@ class WebClientIntegrationTests {
|
||||
Flux<String> result = this.webClient.get()
|
||||
.uri("/greeting")
|
||||
.retrieve()
|
||||
.onStatus(HttpStatus::is5xxServerError, response -> Mono.empty())
|
||||
.onStatus(HttpStatusCode::is5xxServerError, response -> Mono.empty())
|
||||
.bodyToFlux(String.class);
|
||||
|
||||
StepVerifier.create(result)
|
||||
@@ -890,7 +891,7 @@ class WebClientIntegrationTests {
|
||||
Mono<ResponseEntity<String>> result = this.webClient.get()
|
||||
.uri("/").accept(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
.onStatus(HttpStatus::is5xxServerError, response -> Mono.empty())// use normal response
|
||||
.onStatus(HttpStatusCode::is5xxServerError, response -> Mono.empty())// use normal response
|
||||
.toEntity(String.class);
|
||||
|
||||
StepVerifier.create(result)
|
||||
|
||||
@@ -27,6 +27,7 @@ import reactor.test.StepVerifier;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.web.testfixture.server.MockServerWebExchange;
|
||||
@@ -50,13 +51,12 @@ public class RouterFunctionBuilderTests {
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com/foo").build();
|
||||
ServerRequest getRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
Mono<Integer> responseMono = route.route(getRequest)
|
||||
Mono<HttpStatusCode> responseMono = route.route(getRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(getRequest))
|
||||
.map(ServerResponse::statusCode)
|
||||
.map(HttpStatus::value);
|
||||
.map(ServerResponse::statusCode);
|
||||
|
||||
StepVerifier.create(responseMono)
|
||||
.expectNext(200)
|
||||
.expectNext(HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
mockRequest = MockServerHttpRequest.head("https://example.com/foo").build();
|
||||
@@ -65,11 +65,10 @@ public class RouterFunctionBuilderTests {
|
||||
|
||||
responseMono = route.route(headRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(headRequest))
|
||||
.map(ServerResponse::statusCode)
|
||||
.map(HttpStatus::value);
|
||||
.map(ServerResponse::statusCode);
|
||||
|
||||
StepVerifier.create(responseMono)
|
||||
.expectNext(202)
|
||||
.expectNext(HttpStatus.ACCEPTED)
|
||||
.verifyComplete();
|
||||
|
||||
mockRequest = MockServerHttpRequest.post("https://example.com/").
|
||||
@@ -79,11 +78,10 @@ public class RouterFunctionBuilderTests {
|
||||
|
||||
responseMono = route.route(barRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(barRequest))
|
||||
.map(ServerResponse::statusCode)
|
||||
.map(HttpStatus::value);
|
||||
.map(ServerResponse::statusCode);
|
||||
|
||||
StepVerifier.create(responseMono)
|
||||
.expectNext(204)
|
||||
.expectNext(HttpStatus.NO_CONTENT)
|
||||
.verifyComplete();
|
||||
|
||||
mockRequest = MockServerHttpRequest.post("https://example.com/").build();
|
||||
@@ -92,8 +90,7 @@ public class RouterFunctionBuilderTests {
|
||||
|
||||
responseMono = route.route(invalidRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(invalidRequest))
|
||||
.map(ServerResponse::statusCode)
|
||||
.map(HttpStatus::value);
|
||||
.map(ServerResponse::statusCode);
|
||||
|
||||
StepVerifier.create(responseMono)
|
||||
.verifyComplete();
|
||||
@@ -112,13 +109,12 @@ public class RouterFunctionBuilderTests {
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/resources/response.txt").build();
|
||||
ServerRequest resourceRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
Mono<Integer> responseMono = route.route(resourceRequest)
|
||||
Mono<HttpStatusCode> responseMono = route.route(resourceRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(resourceRequest))
|
||||
.map(ServerResponse::statusCode)
|
||||
.map(HttpStatus::value);
|
||||
.map(ServerResponse::statusCode);
|
||||
|
||||
StepVerifier.create(responseMono)
|
||||
.expectNext(200)
|
||||
.expectNext(HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
mockRequest = MockServerHttpRequest.post("https://localhost/resources/foo.txt").build();
|
||||
@@ -126,8 +122,7 @@ public class RouterFunctionBuilderTests {
|
||||
|
||||
responseMono = route.route(invalidRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(invalidRequest))
|
||||
.map(ServerResponse::statusCode)
|
||||
.map(HttpStatus::value);
|
||||
.map(ServerResponse::statusCode);
|
||||
|
||||
StepVerifier.create(responseMono)
|
||||
.verifyComplete();
|
||||
@@ -146,13 +141,12 @@ public class RouterFunctionBuilderTests {
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/foo/bar/baz").build();
|
||||
ServerRequest fooRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
Mono<Integer> responseMono = route.route(fooRequest)
|
||||
Mono<HttpStatusCode> responseMono = route.route(fooRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(fooRequest))
|
||||
.map(ServerResponse::statusCode)
|
||||
.map(HttpStatus::value);
|
||||
.map(ServerResponse::statusCode);
|
||||
|
||||
StepVerifier.create(responseMono)
|
||||
.expectNext(200)
|
||||
.expectNext(HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@@ -201,13 +195,12 @@ public class RouterFunctionBuilderTests {
|
||||
mockRequest = MockServerHttpRequest.get("https://localhost/bar").build();
|
||||
ServerRequest barRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
Mono<Integer> barResponseMono = route.route(barRequest)
|
||||
Mono<HttpStatusCode> barResponseMono = route.route(barRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(barRequest))
|
||||
.map(ServerResponse::statusCode)
|
||||
.map(HttpStatus::value);
|
||||
.map(ServerResponse::statusCode);
|
||||
|
||||
StepVerifier.create(barResponseMono)
|
||||
.expectNext(500)
|
||||
.expectNext(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@@ -222,7 +215,7 @@ public class RouterFunctionBuilderTests {
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com/error").build();
|
||||
ServerRequest serverRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
Mono<HttpStatus> responseStatus = route.route(serverRequest)
|
||||
Mono<HttpStatusCode> responseStatus = route.route(serverRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(serverRequest))
|
||||
.map(ServerResponse::statusCode);
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import reactor.core.publisher.Mono;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
@@ -63,7 +64,7 @@ class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTe
|
||||
ResponseEntity<String> result = this.restTemplate
|
||||
.getForEntity("http://localhost:" + this.port + "/foo/bar", String.class);
|
||||
|
||||
assertThat(result.getStatusCodeValue()).isEqualTo(200);
|
||||
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -329,7 +329,7 @@ public class ResourceWebHandlerTests {
|
||||
StepVerifier.create(handler.handle(exchange))
|
||||
.expectErrorSatisfies(err -> {
|
||||
assertThat(err).isInstanceOf(ResponseStatusException.class);
|
||||
assertThat(((ResponseStatusException) err).getRawStatusCode()).isEqualTo(404);
|
||||
assertThat(((ResponseStatusException) err).getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
}).verify(TIMEOUT);
|
||||
}
|
||||
|
||||
@@ -375,7 +375,7 @@ public class ResourceWebHandlerTests {
|
||||
StepVerifier.create(this.handler.handle(exchange))
|
||||
.expectErrorSatisfies(err -> {
|
||||
assertThat(err).isInstanceOf(ResponseStatusException.class);
|
||||
assertThat(((ResponseStatusException) err).getRawStatusCode()).isEqualTo(404);
|
||||
assertThat(((ResponseStatusException) err).getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
})
|
||||
.verify(TIMEOUT);
|
||||
if (!location.createRelative(requestPath).exists() && !requestPath.contains(":")) {
|
||||
@@ -470,7 +470,7 @@ public class ResourceWebHandlerTests {
|
||||
StepVerifier.create(this.handler.handle(exchange))
|
||||
.expectErrorSatisfies(err -> {
|
||||
assertThat(err).isInstanceOf(ResponseStatusException.class);
|
||||
assertThat(((ResponseStatusException) err).getRawStatusCode()).isEqualTo(404);
|
||||
assertThat(((ResponseStatusException) err).getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
}).verify(TIMEOUT);
|
||||
}
|
||||
|
||||
@@ -481,7 +481,7 @@ public class ResourceWebHandlerTests {
|
||||
StepVerifier.create(this.handler.handle(exchange))
|
||||
.expectErrorSatisfies(err -> {
|
||||
assertThat(err).isInstanceOf(ResponseStatusException.class);
|
||||
assertThat(((ResponseStatusException) err).getRawStatusCode()).isEqualTo(404);
|
||||
assertThat(((ResponseStatusException) err).getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
}).verify(TIMEOUT);
|
||||
}
|
||||
|
||||
@@ -492,7 +492,7 @@ public class ResourceWebHandlerTests {
|
||||
StepVerifier.create(this.handler.handle(exchange))
|
||||
.expectErrorSatisfies(err -> {
|
||||
assertThat(err).isInstanceOf(ResponseStatusException.class);
|
||||
assertThat(((ResponseStatusException) err).getRawStatusCode()).isEqualTo(404);
|
||||
assertThat(((ResponseStatusException) err).getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
}).verify(TIMEOUT);
|
||||
}
|
||||
|
||||
@@ -527,7 +527,7 @@ public class ResourceWebHandlerTests {
|
||||
StepVerifier.create(mono)
|
||||
.expectErrorSatisfies(err -> {
|
||||
assertThat(err).isInstanceOf(ResponseStatusException.class);
|
||||
assertThat(((ResponseStatusException) err).getRawStatusCode()).isEqualTo(404);
|
||||
assertThat(((ResponseStatusException) err).getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
}).verify(TIMEOUT);
|
||||
|
||||
// SPR-17475
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -103,7 +104,7 @@ class RequestMappingExceptionHandlingIntegrationTests extends AbstractRequestMap
|
||||
assertThatExceptionOfType(HttpStatusCodeException.class).isThrownBy(() ->
|
||||
performGet("/SPR-16318", headers, String.class).getBody())
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getRawStatusCode()).isEqualTo(500);
|
||||
assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
assertThat(ex.getResponseHeaders().getContentType().toString()).isEqualTo("application/problem+json");
|
||||
assertThat(ex.getResponseBodyAsString()).isEqualTo("{\"reason\":\"error\"}");
|
||||
});
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
@@ -72,12 +72,12 @@ public class ZeroDemandResponse implements ServerHttpResponse {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean setStatusCode(HttpStatus status) {
|
||||
public boolean setStatusCode(HttpStatusCode status) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpStatus getStatusCode() {
|
||||
public HttpStatusCode getStatusCode() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user