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:
@@ -48,7 +48,7 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
@@ -1125,8 +1125,8 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
|
||||
logger.debug("Exiting from \"" + dispatchType + "\" dispatch, status " + status + headers);
|
||||
}
|
||||
else {
|
||||
HttpStatus httpStatus = HttpStatus.resolve(status);
|
||||
logger.debug("Completed " + (httpStatus != null ? httpStatus : status) + headers);
|
||||
HttpStatusCode httpStatus = HttpStatusCode.valueOf(status);
|
||||
logger.debug("Completed " + httpStatus + headers);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.springframework.web.servlet;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -56,7 +56,7 @@ public class ModelAndView {
|
||||
|
||||
/** Optional HTTP status for the response. */
|
||||
@Nullable
|
||||
private HttpStatus status;
|
||||
private HttpStatusCode status;
|
||||
|
||||
/** Indicates whether or not this instance has been cleared with a call to {@link #clear()}. */
|
||||
private boolean cleared = false;
|
||||
@@ -132,7 +132,7 @@ public class ModelAndView {
|
||||
* (to be set just prior to View rendering)
|
||||
* @since 4.3.8
|
||||
*/
|
||||
public ModelAndView(String viewName, HttpStatus status) {
|
||||
public ModelAndView(String viewName, HttpStatusCode status) {
|
||||
this.view = viewName;
|
||||
this.status = status;
|
||||
}
|
||||
@@ -148,7 +148,7 @@ public class ModelAndView {
|
||||
* (to be set just prior to View rendering)
|
||||
* @since 4.3
|
||||
*/
|
||||
public ModelAndView(@Nullable String viewName, @Nullable Map<String, ?> model, @Nullable HttpStatus status) {
|
||||
public ModelAndView(@Nullable String viewName, @Nullable Map<String, ?> model, @Nullable HttpStatusCode status) {
|
||||
this.view = viewName;
|
||||
if (model != null) {
|
||||
getModelMap().addAllAttributes(model);
|
||||
@@ -264,7 +264,7 @@ public class ModelAndView {
|
||||
* <p>The response status is set just prior to View rendering.
|
||||
* @since 4.3
|
||||
*/
|
||||
public void setStatus(@Nullable HttpStatus status) {
|
||||
public void setStatus(@Nullable HttpStatusCode status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
@@ -273,7 +273,7 @@ public class ModelAndView {
|
||||
* @since 4.3
|
||||
*/
|
||||
@Nullable
|
||||
public HttpStatus getStatus() {
|
||||
public HttpStatusCode getStatus() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.ProblemDetail;
|
||||
import org.springframework.web.ErrorResponse;
|
||||
|
||||
@@ -59,13 +60,12 @@ public class NoHandlerFoundException extends ServletException implements ErrorRe
|
||||
this.httpMethod = httpMethod;
|
||||
this.requestURL = requestURL;
|
||||
this.headers = headers;
|
||||
this.body = ProblemDetail.forRawStatusCode(getRawStatusCode()).withDetail(getMessage());
|
||||
this.body = ProblemDetail.forStatus(getStatusCode()).withDetail(getMessage());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getRawStatusCode() {
|
||||
return HttpStatus.NOT_FOUND.value();
|
||||
public HttpStatusCode getStatusCode() {
|
||||
return HttpStatus.NOT_FOUND;
|
||||
}
|
||||
|
||||
public String getHttpMethod() {
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.springframework.beans.factory.support.ManagedMap;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.mvc.ParameterizableViewController;
|
||||
@@ -74,10 +74,10 @@ class ViewControllerBeanDefinitionParser implements BeanDefinitionParser {
|
||||
RootBeanDefinition controller = new RootBeanDefinition(ParameterizableViewController.class);
|
||||
controller.setSource(source);
|
||||
|
||||
HttpStatus statusCode = null;
|
||||
HttpStatusCode statusCode = null;
|
||||
if (element.hasAttribute("status-code")) {
|
||||
int statusValue = Integer.parseInt(element.getAttribute("status-code"));
|
||||
statusCode = HttpStatus.valueOf(statusValue);
|
||||
statusCode = HttpStatusCode.valueOf(statusValue);
|
||||
}
|
||||
|
||||
String name = element.getLocalName();
|
||||
@@ -130,7 +130,7 @@ class ViewControllerBeanDefinitionParser implements BeanDefinitionParser {
|
||||
return beanDef;
|
||||
}
|
||||
|
||||
private RootBeanDefinition getRedirectView(Element element, @Nullable HttpStatus status, @Nullable Object source) {
|
||||
private RootBeanDefinition getRedirectView(Element element, @Nullable HttpStatusCode status, @Nullable Object source) {
|
||||
RootBeanDefinition redirectView = new RootBeanDefinition(RedirectView.class);
|
||||
redirectView.setSource(source);
|
||||
redirectView.getConstructorArgumentValues().addIndexedArgumentValue(0, element.getAttribute("redirect-url"));
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.servlet.mvc.ParameterizableViewController;
|
||||
@@ -53,7 +53,7 @@ public class RedirectViewControllerRegistration {
|
||||
* <p>If not set, {@link org.springframework.web.servlet.view.RedirectView}
|
||||
* will select {@code HttpStatus.MOVED_TEMPORARILY (302)} by default.
|
||||
*/
|
||||
public RedirectViewControllerRegistration setStatusCode(HttpStatus statusCode) {
|
||||
public RedirectViewControllerRegistration setStatusCode(HttpStatusCode statusCode) {
|
||||
Assert.isTrue(statusCode.is3xxRedirection(), "Not a redirect status code");
|
||||
this.redirectView.setStatusCode(statusCode);
|
||||
return this;
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.servlet.RequestToViewNameTranslator;
|
||||
@@ -47,7 +47,7 @@ public class ViewControllerRegistration {
|
||||
* Set the status code to set on the response. Optional.
|
||||
* <p>If not set the response status will be 200 (OK).
|
||||
*/
|
||||
public ViewControllerRegistration setStatusCode(HttpStatus statusCode) {
|
||||
public ViewControllerRegistration setStatusCode(HttpStatusCode statusCode) {
|
||||
this.controller.setStatusCode(statusCode);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
@@ -104,7 +104,7 @@ public class ViewControllerRegistry {
|
||||
* {@link PathPattern} more tailored for web usage and more efficient.
|
||||
* @since 4.1
|
||||
*/
|
||||
public void addStatusController(String urlPath, HttpStatus statusCode) {
|
||||
public void addStatusController(String urlPath, HttpStatusCode statusCode) {
|
||||
ViewControllerRegistration registration = new ViewControllerRegistration(urlPath);
|
||||
registration.setApplicationContext(this.applicationContext);
|
||||
registration.setStatusCode(statusCode);
|
||||
|
||||
@@ -27,7 +27,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
@@ -45,14 +45,14 @@ abstract class AbstractServerResponse extends ErrorHandlingServerResponse {
|
||||
|
||||
private static final Set<HttpMethod> SAFE_METHODS = Set.of(HttpMethod.GET, HttpMethod.HEAD);
|
||||
|
||||
final int statusCode;
|
||||
private final HttpStatusCode statusCode;
|
||||
|
||||
private final HttpHeaders headers;
|
||||
|
||||
private final MultiValueMap<String, Cookie> cookies;
|
||||
|
||||
protected AbstractServerResponse(
|
||||
int statusCode, HttpHeaders headers, MultiValueMap<String, Cookie> cookies) {
|
||||
HttpStatusCode statusCode, HttpHeaders headers, MultiValueMap<String, Cookie> cookies) {
|
||||
|
||||
this.statusCode = statusCode;
|
||||
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
|
||||
@@ -61,13 +61,14 @@ abstract class AbstractServerResponse extends ErrorHandlingServerResponse {
|
||||
}
|
||||
|
||||
@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
|
||||
@@ -104,7 +105,7 @@ abstract class AbstractServerResponse extends ErrorHandlingServerResponse {
|
||||
}
|
||||
|
||||
private void writeStatusAndHeaders(HttpServletResponse response) {
|
||||
response.setStatus(this.statusCode);
|
||||
response.setStatus(this.statusCode.value());
|
||||
writeHeaders(response);
|
||||
writeCookies(response);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ import org.reactivestreams.Publisher;
|
||||
import org.springframework.core.ReactiveAdapter;
|
||||
import org.springframework.core.ReactiveAdapterRegistry;
|
||||
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.ClassUtils;
|
||||
@@ -83,11 +83,12 @@ final class DefaultAsyncServerResponse extends ErrorHandlingServerResponse imple
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpStatus statusCode() {
|
||||
public HttpStatusCode statusCode() {
|
||||
return delegate(ServerResponse::statusCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public int rawStatusCode() {
|
||||
return delegate(ServerResponse::rawStatusCode);
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@ 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.InvalidMediaTypeException;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.GenericHttpMessageConverter;
|
||||
@@ -80,7 +81,7 @@ final class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T>
|
||||
|
||||
private final Type entityType;
|
||||
|
||||
private int status = HttpStatus.OK.value();
|
||||
private HttpStatusCode status = HttpStatus.OK;
|
||||
|
||||
private final HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
@@ -93,16 +94,15 @@ final 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
|
||||
@@ -241,7 +241,7 @@ final class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T>
|
||||
|
||||
private final Type entityType;
|
||||
|
||||
public DefaultEntityResponse(int statusCode, HttpHeaders headers,
|
||||
public DefaultEntityResponse(HttpStatusCode statusCode, HttpHeaders headers,
|
||||
MultiValueMap<String, Cookie> cookies, T entity, Type entityType) {
|
||||
|
||||
super(statusCode, headers, cookies);
|
||||
@@ -347,7 +347,7 @@ final class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T>
|
||||
*/
|
||||
private static class CompletionStageEntityResponse<T> extends DefaultEntityResponse<CompletionStage<T>> {
|
||||
|
||||
public CompletionStageEntityResponse(int statusCode, HttpHeaders headers,
|
||||
public CompletionStageEntityResponse(HttpStatusCode statusCode, HttpHeaders headers,
|
||||
MultiValueMap<String, Cookie> cookies, CompletionStage<T> entity, Type entityType) {
|
||||
|
||||
super(statusCode, headers, cookies, entity, entityType);
|
||||
@@ -401,7 +401,7 @@ final class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T>
|
||||
*/
|
||||
private static class PublisherEntityResponse<T> extends DefaultEntityResponse<Publisher<T>> {
|
||||
|
||||
public PublisherEntityResponse(int statusCode, HttpHeaders headers,
|
||||
public PublisherEntityResponse(HttpStatusCode statusCode, HttpHeaders headers,
|
||||
MultiValueMap<String, Cookie> cookies, Publisher<T> entity, Type entityType) {
|
||||
|
||||
super(statusCode, headers, cookies, entity, entityType);
|
||||
|
||||
@@ -30,6 +30,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.core.Conventions;
|
||||
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.LinkedMultiValueMap;
|
||||
@@ -46,7 +47,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();
|
||||
|
||||
@@ -58,8 +59,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());
|
||||
}
|
||||
@@ -71,16 +71,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
|
||||
@@ -156,7 +155,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, Cookie> cookies, String name, Map<String, Object> model) {
|
||||
|
||||
super(statusCode, headers, cookies);
|
||||
@@ -178,14 +177,7 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder
|
||||
protected ModelAndView writeToInternal(HttpServletRequest request,
|
||||
HttpServletResponse response, Context context) {
|
||||
|
||||
HttpStatus status = HttpStatus.resolve(this.statusCode);
|
||||
ModelAndView mav;
|
||||
if (status != null) {
|
||||
mav = new ModelAndView(this.name, status);
|
||||
}
|
||||
else {
|
||||
mav = new ModelAndView(this.name);
|
||||
}
|
||||
ModelAndView mav = new ModelAndView(this.name, this.statusCode());
|
||||
mav.addAllObjects(this.model);
|
||||
return mav;
|
||||
}
|
||||
|
||||
@@ -34,7 +34,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.util.Assert;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
@@ -49,7 +49,7 @@ import org.springframework.web.servlet.ModelAndView;
|
||||
*/
|
||||
class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||
|
||||
private final int statusCode;
|
||||
private final HttpStatusCode statusCode;
|
||||
|
||||
private final HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
@@ -58,19 +58,14 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||
|
||||
public DefaultServerResponseBuilder(ServerResponse other) {
|
||||
Assert.notNull(other, "ServerResponse must not be null");
|
||||
this.statusCode = (other instanceof AbstractServerResponse ?
|
||||
((AbstractServerResponse) other).statusCode : other.statusCode().value());
|
||||
this.statusCode = other.statusCode();
|
||||
this.headers.addAll(other.headers());
|
||||
this.cookies.addAll(other.cookies());
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -221,7 +216,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||
|
||||
private final BiFunction<HttpServletRequest, HttpServletResponse, ModelAndView> writeFunction;
|
||||
|
||||
public WriterFunctionResponse(int statusCode, HttpHeaders headers, MultiValueMap<String, Cookie> cookies,
|
||||
public WriterFunctionResponse(HttpStatusCode statusCode, HttpHeaders headers, MultiValueMap<String, Cookie> cookies,
|
||||
BiFunction<HttpServletRequest, HttpServletResponse, ModelAndView> writeFunction) {
|
||||
|
||||
super(statusCode, headers, cookies);
|
||||
|
||||
@@ -28,7 +28,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.util.MultiValueMap;
|
||||
|
||||
@@ -102,7 +102,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 jakarta.servlet.http.Cookie;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
@@ -135,7 +135,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.
|
||||
|
||||
@@ -42,6 +42,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.converter.HttpMessageConverter;
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -60,18 +61,16 @@ 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
|
||||
* @see #statusCode()
|
||||
* @see HttpStatus#valueOf(int)
|
||||
* @deprecated as of 6.0, in favor of {@link #statusCode()}
|
||||
*/
|
||||
@Deprecated
|
||||
int rawStatusCode();
|
||||
|
||||
/**
|
||||
@@ -112,7 +111,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);
|
||||
}
|
||||
|
||||
@@ -122,7 +121,7 @@ public interface ServerResponse {
|
||||
* @return the created builder
|
||||
*/
|
||||
static BodyBuilder status(int status) {
|
||||
return new DefaultServerResponseBuilder(status);
|
||||
return new DefaultServerResponseBuilder(HttpStatusCode.valueOf(status));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -31,6 +31,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.http.CacheControl;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.server.DelegatingServerHttpResponse;
|
||||
@@ -59,7 +60,7 @@ final class SseServerResponse extends AbstractServerResponse {
|
||||
|
||||
|
||||
private SseServerResponse(Consumer<SseBuilder> sseConsumer, @Nullable Duration timeout) {
|
||||
super(200, createHeaders(), emptyCookies());
|
||||
super(HttpStatus.OK, createHeaders(), emptyCookies());
|
||||
this.sseConsumer = sseConsumer;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.View;
|
||||
@@ -42,7 +43,7 @@ public class ParameterizableViewController extends AbstractController {
|
||||
private Object view;
|
||||
|
||||
@Nullable
|
||||
private HttpStatus statusCode;
|
||||
private HttpStatusCode statusCode;
|
||||
|
||||
private boolean statusOnly;
|
||||
|
||||
@@ -108,7 +109,7 @@ public class ParameterizableViewController extends AbstractController {
|
||||
* fully handled within the controller.
|
||||
* @since 4.1
|
||||
*/
|
||||
public void setStatusCode(@Nullable HttpStatus statusCode) {
|
||||
public void setStatusCode(@Nullable HttpStatusCode statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
@@ -117,7 +118,7 @@ public class ParameterizableViewController extends AbstractController {
|
||||
* @since 4.1
|
||||
*/
|
||||
@Nullable
|
||||
public HttpStatus getStatusCode() {
|
||||
public HttpStatusCode getStatusCode() {
|
||||
return this.statusCode;
|
||||
}
|
||||
|
||||
|
||||
@@ -131,7 +131,7 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes
|
||||
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws Exception {
|
||||
|
||||
ex.getHeaders().forEach((name, values) -> values.forEach(value -> response.addHeader(name, value)));
|
||||
return applyStatusAndReason(ex.getRawStatusCode(), ex.getReason(), response);
|
||||
return applyStatusAndReason(ex.getStatusCode().value(), ex.getReason(), response);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,7 +33,7 @@ import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.core.SpringProperties;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
@@ -442,7 +442,7 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
|
||||
}
|
||||
else {
|
||||
ModelMap model = mavContainer.getModel();
|
||||
HttpStatus status = mavContainer.getStatus();
|
||||
HttpStatusCode status = mavContainer.getStatus();
|
||||
ModelAndView mav = new ModelAndView(mavContainer.getViewName(), model, status);
|
||||
mav.setViewName(mavContainer.getViewName());
|
||||
if (!mavContainer.isViewReference()) {
|
||||
|
||||
@@ -182,7 +182,7 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
|
||||
|
||||
HttpEntity<?> httpEntity;
|
||||
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());
|
||||
@@ -216,7 +216,7 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
|
||||
}
|
||||
|
||||
if (httpEntity instanceof ResponseEntity<?> responseEntity) {
|
||||
int returnStatus = responseEntity.getStatusCodeValue();
|
||||
int returnStatus = responseEntity.getStatusCode().value();
|
||||
outputMessage.getServletResponse().setStatus(returnStatus);
|
||||
if (returnStatus == 200) {
|
||||
HttpMethod method = inputMessage.getMethod();
|
||||
|
||||
@@ -130,9 +130,8 @@ public class ResponseBodyEmitterReturnValueHandler implements HandlerMethodRetur
|
||||
Assert.state(response != null, "No HttpServletResponse");
|
||||
ServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
|
||||
|
||||
if (returnValue instanceof ResponseEntity) {
|
||||
ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
|
||||
response.setStatus(responseEntity.getStatusCodeValue());
|
||||
if (returnValue instanceof ResponseEntity<?> responseEntity) {
|
||||
response.setStatus(responseEntity.getStatusCode().value());
|
||||
outputMessage.getHeaders().putAll(responseEntity.getHeaders());
|
||||
returnValue = responseEntity.getBody();
|
||||
returnType = returnType.nested();
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.springframework.beans.ConversionNotSupportedException;
|
||||
import org.springframework.beans.TypeMismatchException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
@@ -124,62 +125,57 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
|
||||
if (ex instanceof ErrorResponse errorEx) {
|
||||
if (ex instanceof HttpRequestMethodNotSupportedException subEx) {
|
||||
return handleHttpRequestMethodNotSupported(subEx, subEx.getHeaders(), subEx.getStatus(), request);
|
||||
return handleHttpRequestMethodNotSupported(subEx, subEx.getHeaders(), subEx.getStatusCode(), request);
|
||||
}
|
||||
else if (ex instanceof HttpMediaTypeNotSupportedException subEx) {
|
||||
return handleHttpMediaTypeNotSupported(subEx, subEx.getHeaders(), subEx.getStatus(), request);
|
||||
return handleHttpMediaTypeNotSupported(subEx, subEx.getHeaders(), subEx.getStatusCode(), request);
|
||||
}
|
||||
else if (ex instanceof HttpMediaTypeNotAcceptableException subEx) {
|
||||
return handleHttpMediaTypeNotAcceptable(subEx, subEx.getHeaders(), subEx.getStatus(), request);
|
||||
return handleHttpMediaTypeNotAcceptable(subEx, subEx.getHeaders(), subEx.getStatusCode(), request);
|
||||
}
|
||||
else if (ex instanceof MissingPathVariableException subEx) {
|
||||
return handleMissingPathVariable(subEx, subEx.getHeaders(), subEx.getStatus(), request);
|
||||
return handleMissingPathVariable(subEx, subEx.getHeaders(), subEx.getStatusCode(), request);
|
||||
}
|
||||
else if (ex instanceof MissingServletRequestParameterException subEx) {
|
||||
return handleMissingServletRequestParameter(subEx, subEx.getHeaders(), subEx.getStatus(), request);
|
||||
return handleMissingServletRequestParameter(subEx, subEx.getHeaders(), subEx.getStatusCode(), request);
|
||||
}
|
||||
else if (ex instanceof MissingServletRequestPartException subEx) {
|
||||
return handleMissingServletRequestPart(subEx, subEx.getHeaders(), subEx.getStatus(), request);
|
||||
return handleMissingServletRequestPart(subEx, subEx.getHeaders(), subEx.getStatusCode(), request);
|
||||
}
|
||||
else if (ex instanceof ServletRequestBindingException subEx) {
|
||||
return handleServletRequestBindingException(subEx, subEx.getHeaders(), subEx.getStatus(), request);
|
||||
return handleServletRequestBindingException(subEx, subEx.getHeaders(), subEx.getStatusCode(), request);
|
||||
}
|
||||
else if (ex instanceof MethodArgumentNotValidException subEx) {
|
||||
return handleMethodArgumentNotValid(subEx, subEx.getHeaders(), subEx.getStatus(), request);
|
||||
return handleMethodArgumentNotValid(subEx, subEx.getHeaders(), subEx.getStatusCode(), request);
|
||||
}
|
||||
else if (ex instanceof NoHandlerFoundException subEx) {
|
||||
return handleNoHandlerFoundException(subEx, subEx.getHeaders(), subEx.getStatus(), request);
|
||||
return handleNoHandlerFoundException(subEx, subEx.getHeaders(), subEx.getStatusCode(), request);
|
||||
}
|
||||
else if (ex instanceof AsyncRequestTimeoutException subEx) {
|
||||
return handleAsyncRequestTimeoutException(subEx, subEx.getHeaders(), subEx.getStatus(), request);
|
||||
return handleAsyncRequestTimeoutException(subEx, subEx.getHeaders(), subEx.getStatusCode(), request);
|
||||
}
|
||||
else {
|
||||
// Another ErrorResponseException
|
||||
return handleExceptionInternal(ex, null, errorEx.getHeaders(), errorEx.getStatus(), request);
|
||||
return handleExceptionInternal(ex, null, errorEx.getHeaders(), errorEx.getStatusCode(), request);
|
||||
}
|
||||
}
|
||||
|
||||
// Other, lower level exceptions
|
||||
|
||||
if (ex instanceof ConversionNotSupportedException) {
|
||||
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
return handleConversionNotSupported((ConversionNotSupportedException) ex, headers, status, request);
|
||||
if (ex instanceof ConversionNotSupportedException cnse) {
|
||||
return handleConversionNotSupported(cnse, headers, HttpStatus.INTERNAL_SERVER_ERROR, request);
|
||||
}
|
||||
else if (ex instanceof TypeMismatchException) {
|
||||
HttpStatus status = HttpStatus.BAD_REQUEST;
|
||||
return handleTypeMismatch((TypeMismatchException) ex, headers, status, request);
|
||||
else if (ex instanceof TypeMismatchException tme) {
|
||||
return handleTypeMismatch(tme, headers, HttpStatus.BAD_REQUEST, request);
|
||||
}
|
||||
else if (ex instanceof HttpMessageNotReadableException) {
|
||||
HttpStatus status = HttpStatus.BAD_REQUEST;
|
||||
return handleHttpMessageNotReadable((HttpMessageNotReadableException) ex, headers, status, request);
|
||||
else if (ex instanceof HttpMessageNotReadableException hmnre) {
|
||||
return handleHttpMessageNotReadable(hmnre, headers, HttpStatus.BAD_REQUEST, request);
|
||||
}
|
||||
else if (ex instanceof HttpMessageNotWritableException) {
|
||||
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
return handleHttpMessageNotWritable((HttpMessageNotWritableException) ex, headers, status, request);
|
||||
else if (ex instanceof HttpMessageNotWritableException hmnwe) {
|
||||
return handleHttpMessageNotWritable(hmnwe, headers, HttpStatus.INTERNAL_SERVER_ERROR, request);
|
||||
}
|
||||
else if (ex instanceof BindException) {
|
||||
HttpStatus status = HttpStatus.BAD_REQUEST;
|
||||
return handleBindException((BindException) ex, headers, status, request);
|
||||
else if (ex instanceof BindException be) {
|
||||
return handleBindException(be, headers, HttpStatus.BAD_REQUEST, request);
|
||||
}
|
||||
else {
|
||||
// Unknown exception, typically a wrapper with a common MVC exception as cause
|
||||
@@ -201,7 +197,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(
|
||||
HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
pageNotFoundLogger.warn(ex.getMessage());
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
@@ -218,7 +214,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(
|
||||
HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
}
|
||||
@@ -234,7 +230,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(
|
||||
HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
}
|
||||
@@ -251,7 +247,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleMissingPathVariable(
|
||||
MissingPathVariableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
MissingPathVariableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
}
|
||||
@@ -267,7 +263,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleMissingServletRequestParameter(
|
||||
MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
}
|
||||
@@ -283,7 +279,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleMissingServletRequestPart(
|
||||
MissingServletRequestPartException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
MissingServletRequestPartException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
}
|
||||
@@ -299,7 +295,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleServletRequestBindingException(
|
||||
ServletRequestBindingException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
ServletRequestBindingException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
}
|
||||
@@ -315,7 +311,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleMethodArgumentNotValid(
|
||||
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
}
|
||||
@@ -332,7 +328,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleNoHandlerFoundException(
|
||||
NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
NoHandlerFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
}
|
||||
@@ -349,7 +345,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleAsyncRequestTimeoutException(
|
||||
AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatus status, WebRequest webRequest) {
|
||||
AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatusCode status, WebRequest webRequest) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, webRequest);
|
||||
}
|
||||
@@ -365,7 +361,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleConversionNotSupported(
|
||||
ConversionNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
ConversionNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
}
|
||||
@@ -381,7 +377,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleTypeMismatch(
|
||||
TypeMismatchException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
}
|
||||
@@ -397,7 +393,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleHttpMessageNotReadable(
|
||||
HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
}
|
||||
@@ -413,7 +409,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleHttpMessageNotWritable(
|
||||
HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
}
|
||||
@@ -429,7 +425,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleBindException(
|
||||
BindException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
BindException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
return handleExceptionInternal(ex, null, headers, status, request);
|
||||
}
|
||||
@@ -442,13 +438,13 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
* @param ex the exception
|
||||
* @param body the body for the response
|
||||
* @param headers the headers for the response
|
||||
* @param status the response status
|
||||
* @param statusCode the response status
|
||||
* @param webRequest the current request
|
||||
* @return {@code ResponseEntity} or {@code null} if response is committed
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleExceptionInternal(
|
||||
Exception ex, @Nullable Object body, HttpHeaders headers, HttpStatus status, WebRequest webRequest) {
|
||||
Exception ex, @Nullable Object body, HttpHeaders headers, HttpStatusCode statusCode, WebRequest webRequest) {
|
||||
|
||||
if (webRequest instanceof ServletWebRequest servletWebRequest) {
|
||||
HttpServletResponse response = servletWebRequest.getResponse();
|
||||
@@ -460,7 +456,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
}
|
||||
}
|
||||
|
||||
if (HttpStatus.INTERNAL_SERVER_ERROR.equals(status)) {
|
||||
if (HttpStatus.INTERNAL_SERVER_ERROR.equals(statusCode)) {
|
||||
webRequest.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, ex, WebRequest.SCOPE_REQUEST);
|
||||
}
|
||||
|
||||
@@ -468,7 +464,7 @@ public abstract class ResponseEntityExceptionHandler {
|
||||
body = errorResponse.getBody();
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(body, headers, status);
|
||||
return new ResponseEntity<>(body, headers, statusCode);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ import org.springframework.core.KotlinDetector;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
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.ClassUtils;
|
||||
@@ -147,7 +147,7 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
|
||||
* Set the response status according to the {@link ResponseStatus} annotation.
|
||||
*/
|
||||
private void setResponseStatus(ServletWebRequest webRequest) throws IOException {
|
||||
HttpStatus status = getResponseStatus();
|
||||
HttpStatusCode status = getResponseStatus();
|
||||
if (status == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -71,9 +71,8 @@ public class StreamingResponseBodyReturnValueHandler implements HandlerMethodRet
|
||||
Assert.state(response != null, "No HttpServletResponse");
|
||||
ServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
|
||||
|
||||
if (returnValue instanceof ResponseEntity) {
|
||||
ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
|
||||
response.setStatus(responseEntity.getStatusCodeValue());
|
||||
if (returnValue instanceof ResponseEntity<?> responseEntity) {
|
||||
response.setStatus(responseEntity.getStatusCode().value());
|
||||
outputMessage.getHeaders().putAll(responseEntity.getHeaders());
|
||||
returnValue = responseEntity.getBody();
|
||||
if (returnValue == null) {
|
||||
|
||||
@@ -460,7 +460,7 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
|
||||
HttpHeaders headers = errorResponse.getHeaders();
|
||||
headers.forEach((name, values) -> values.forEach(value -> response.addHeader(name, value)));
|
||||
|
||||
int status = errorResponse.getRawStatusCode();
|
||||
int status = errorResponse.getStatusCode().value();
|
||||
String message = errorResponse.getBody().getDetail();
|
||||
if (message != null) {
|
||||
response.sendError(status, message);
|
||||
|
||||
@@ -32,6 +32,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
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;
|
||||
@@ -98,7 +99,7 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView {
|
||||
private String encodingScheme;
|
||||
|
||||
@Nullable
|
||||
private HttpStatus statusCode;
|
||||
private HttpStatusCode statusCode;
|
||||
|
||||
private boolean expandUriTemplateVariables = true;
|
||||
|
||||
@@ -222,7 +223,7 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView {
|
||||
* <p>Default is to send 302/303, depending on the value of the
|
||||
* {@link #setHttp10Compatible(boolean) http10Compatible} flag.
|
||||
*/
|
||||
public void setStatusCode(HttpStatus statusCode) {
|
||||
public void setStatusCode(HttpStatusCode statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
@@ -613,7 +614,7 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView {
|
||||
|
||||
String encodedURL = (isRemoteHost(targetUrl) ? targetUrl : response.encodeRedirectURL(targetUrl));
|
||||
if (http10Compatible) {
|
||||
HttpStatus attributeStatusCode = (HttpStatus) request.getAttribute(View.RESPONSE_STATUS_ATTRIBUTE);
|
||||
HttpStatusCode attributeStatusCode = (HttpStatusCode) request.getAttribute(View.RESPONSE_STATUS_ATTRIBUTE);
|
||||
if (this.statusCode != null) {
|
||||
response.setStatus(this.statusCode.value());
|
||||
response.setHeader("Location", encodedURL);
|
||||
@@ -628,7 +629,7 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView {
|
||||
}
|
||||
}
|
||||
else {
|
||||
HttpStatus statusCode = getHttp11StatusCode(request, response, targetUrl);
|
||||
HttpStatusCode statusCode = getHttp11StatusCode(request, response, targetUrl);
|
||||
response.setStatus(statusCode.value());
|
||||
response.setHeader("Location", encodedURL);
|
||||
}
|
||||
@@ -662,7 +663,7 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView {
|
||||
|
||||
/**
|
||||
* Determines the status code to use for HTTP 1.1 compatible requests.
|
||||
* <p>The default implementation returns the {@link #setStatusCode(HttpStatus) statusCode}
|
||||
* <p>The default implementation returns the {@link #setStatusCode(HttpStatusCode) statusCode}
|
||||
* property if set, or the value of the {@link #RESPONSE_STATUS_ATTRIBUTE} attribute.
|
||||
* If neither are set, it defaults to {@link HttpStatus#SEE_OTHER} (303).
|
||||
* @param request the request to inspect
|
||||
@@ -670,13 +671,13 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView {
|
||||
* @param targetUrl the target URL
|
||||
* @return the response status
|
||||
*/
|
||||
protected HttpStatus getHttp11StatusCode(
|
||||
protected HttpStatusCode getHttp11StatusCode(
|
||||
HttpServletRequest request, HttpServletResponse response, String targetUrl) {
|
||||
|
||||
if (this.statusCode != null) {
|
||||
return this.statusCode;
|
||||
}
|
||||
HttpStatus attributeStatusCode = (HttpStatus) request.getAttribute(View.RESPONSE_STATUS_ATTRIBUTE);
|
||||
HttpStatusCode attributeStatusCode = (HttpStatusCode) request.getAttribute(View.RESPONSE_STATUS_ATTRIBUTE);
|
||||
if (attributeStatusCode != null) {
|
||||
return attributeStatusCode;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.springframework.web.servlet.function
|
||||
|
||||
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 java.net.URI
|
||||
import java.util.*
|
||||
@@ -767,7 +767,7 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD
|
||||
/**
|
||||
* @see ServerResponse.status
|
||||
*/
|
||||
fun status(status: HttpStatus) = ServerResponse.status(status)
|
||||
fun status(status: HttpStatusCode) = ServerResponse.status(status)
|
||||
|
||||
/**
|
||||
* @see ServerResponse.status
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.junit.jupiter.api.Test;
|
||||
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.lang.Nullable;
|
||||
import org.springframework.web.servlet.handler.PathPatternsTestUtils;
|
||||
@@ -53,7 +54,7 @@ class RouterFunctionBuilderTests {
|
||||
|
||||
ServerRequest getFooRequest = initRequest("GET", "/foo");
|
||||
|
||||
Optional<HttpStatus> responseStatus = route.route(getFooRequest)
|
||||
Optional<HttpStatusCode> responseStatus = route.route(getFooRequest)
|
||||
.map(handlerFunction -> handle(handlerFunction, getFooRequest))
|
||||
.map(ServerResponse::statusCode);
|
||||
assertThat(responseStatus).contains(HttpStatus.OK);
|
||||
@@ -102,7 +103,7 @@ class RouterFunctionBuilderTests {
|
||||
|
||||
ServerRequest resourceRequest = initRequest("GET", "/resources/response.txt");
|
||||
|
||||
Optional<HttpStatus> responseStatus = route.route(resourceRequest)
|
||||
Optional<HttpStatusCode> responseStatus = route.route(resourceRequest)
|
||||
.map(handlerFunction -> handle(handlerFunction, resourceRequest))
|
||||
.map(ServerResponse::statusCode);
|
||||
assertThat(responseStatus).contains(HttpStatus.OK);
|
||||
@@ -127,7 +128,7 @@ class RouterFunctionBuilderTests {
|
||||
|
||||
ServerRequest fooRequest = initRequest("GET", "/foo/bar/baz");
|
||||
|
||||
Optional<HttpStatus> responseStatus = route.route(fooRequest)
|
||||
Optional<HttpStatusCode> responseStatus = route.route(fooRequest)
|
||||
.map(handlerFunction -> handle(handlerFunction, fooRequest))
|
||||
.map(ServerResponse::statusCode);
|
||||
assertThat(responseStatus).contains(HttpStatus.OK);
|
||||
@@ -175,7 +176,7 @@ class RouterFunctionBuilderTests {
|
||||
|
||||
ServerRequest barRequest = initRequest("GET", "/bar");
|
||||
|
||||
Optional<HttpStatus> responseStatus = route.route(barRequest)
|
||||
Optional<HttpStatusCode> responseStatus = route.route(barRequest)
|
||||
.map(handlerFunction -> handle(handlerFunction, barRequest))
|
||||
.map(ServerResponse::statusCode);
|
||||
assertThat(responseStatus).contains(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
@@ -194,7 +195,7 @@ class RouterFunctionBuilderTests {
|
||||
MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/error");
|
||||
ServerRequest serverRequest = new DefaultServerRequest(servletRequest, emptyList());
|
||||
|
||||
Optional<HttpStatus> responseStatus = route.route(serverRequest)
|
||||
Optional<HttpStatusCode> responseStatus = route.route(serverRequest)
|
||||
.map(handlerFunction -> handle(handlerFunction, serverRequest))
|
||||
.map(ServerResponse::statusCode);
|
||||
assertThat(responseStatus).contains(HttpStatus.OK);
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.springframework.core.MethodParameter;
|
||||
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.converter.HttpMessageNotReadableException;
|
||||
@@ -330,7 +331,7 @@ public class ResponseEntityExceptionHandlerTests {
|
||||
|
||||
@Override
|
||||
protected ResponseEntity<Object> handleServletRequestBindingException(
|
||||
ServletRequestBindingException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
ServletRequestBindingException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
|
||||
headers = new HttpHeaders();
|
||||
headers.set("someHeader", "someHeaderValue");
|
||||
|
||||
Reference in New Issue
Block a user