Browse Source

Align HttpStatus with RFC9110

This commit updates the `HttpStatus` enum with the latest changes in
RFC9110:

* deprecate "413 Payload Too Large" in favor of "413 Content Too Large"
* deprecate "418 I'm a teapot" as it was meant as a joke and is now
  marked as unused
* Introduce new "421 Misdirected Request"
* deprecate "422 Unprocessable Entity" in favor of
  "422 Unprocessable Content"
* deprecate "509 Bandwidth Limit Exceeded" as it's now unassigned
* deprecate "510 Not Extended" as it's now marked as "historic"

The relevant exceptions, test matchers and more have been updated as a
result.

Closes gh-32870
pull/35392/head
Brian Clozel 4 months ago
parent
commit
7112efee1b
  1. 40
      spring-test/src/main/java/org/springframework/test/web/servlet/result/StatusResultMatchers.java
  2. 35
      spring-test/src/main/kotlin/org/springframework/test/web/servlet/result/StatusResultMatchersDsl.kt
  3. 127
      spring-web/src/main/java/org/springframework/http/HttpStatus.java
  4. 12
      spring-web/src/main/java/org/springframework/http/ResponseEntity.java
  5. 23
      spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java
  6. 4
      spring-web/src/main/java/org/springframework/web/multipart/MaxUploadSizeExceededException.java
  7. 37
      spring-web/src/main/java/org/springframework/web/server/ContentTooLargeException.java
  8. 2
      spring-web/src/main/java/org/springframework/web/server/PayloadTooLargeException.java
  9. 146
      spring-web/src/test/java/org/springframework/http/HttpStatusTests.java
  10. 10
      spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java
  11. 18
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java
  12. 11
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java
  13. 4
      spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageReaderArgumentResolver.java
  14. 7
      spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDsl.kt
  15. 11
      spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt
  16. 4
      spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageReaderArgumentResolverTests.java
  17. 1
      spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityExceptionHandlerTests.java
  18. 11
      spring-webmvc/src/main/java/org/springframework/web/servlet/function/ServerResponse.java
  19. 7
      spring-webmvc/src/main/kotlin/org/springframework/web/servlet/function/RouterFunctionDsl.kt

40
spring-test/src/main/java/org/springframework/test/web/servlet/result/StatusResultMatchers.java

@ -141,7 +141,9 @@ public class StatusResultMatchers {
/** /**
* Assert the response status code is {@code HttpStatus.PROCESSING} (102). * Assert the response status code is {@code HttpStatus.PROCESSING} (102).
* @deprecated since 7.0, removed from <a href="https://datatracker.ietf.org/doc/html/rfc4918#section-21.4">WebDAV specification</a>
*/ */
@Deprecated(since = "7.0")
public ResultMatcher isProcessing() { public ResultMatcher isProcessing() {
return matcher(HttpStatus.PROCESSING); return matcher(HttpStatus.PROCESSING);
} }
@ -364,10 +366,20 @@ public class StatusResultMatchers {
return matcher(HttpStatus.PRECONDITION_FAILED); return matcher(HttpStatus.PRECONDITION_FAILED);
} }
/**
* Assert the response status code is {@code HttpStatus.CONTENT_TOO_LARGE} (413).
* @since 7.0
*/
public ResultMatcher isContentTooLarge() {
return matcher(HttpStatus.CONTENT_TOO_LARGE);
}
/** /**
* Assert the response status code is {@code HttpStatus.PAYLOAD_TOO_LARGE} (413). * Assert the response status code is {@code HttpStatus.PAYLOAD_TOO_LARGE} (413).
* @since 4.1 * @since 4.1
* @deprecated since 7.0 in favor of {@link #isContentTooLarge()}
*/ */
@Deprecated(since = "7.0")
public ResultMatcher isPayloadTooLarge() { public ResultMatcher isPayloadTooLarge() {
return matcher(HttpStatus.PAYLOAD_TOO_LARGE); return matcher(HttpStatus.PAYLOAD_TOO_LARGE);
} }
@ -403,14 +415,34 @@ public class StatusResultMatchers {
/** /**
* Assert the response status code is {@code HttpStatus.I_AM_A_TEAPOT} (418). * Assert the response status code is {@code HttpStatus.I_AM_A_TEAPOT} (418).
* @deprecated since 7.0, this was marked as unused in RFC 9110
*/ */
@Deprecated(since = "7.0")
public ResultMatcher isIAmATeapot() { public ResultMatcher isIAmATeapot() {
return matcher(HttpStatus.valueOf(418)); return matcher(HttpStatus.I_AM_A_TEAPOT);
}
/**
* Assert the response status code is {@code HttpStatus.MISDIRECTED_REQUEST} (421).
* @since 7.0
*/
public ResultMatcher isMisdirectedRequest() {
return matcher(HttpStatus.MISDIRECTED_REQUEST);
}
/**
* Assert the response status code is {@code HttpStatus.UNPROCESSABLE_CONTENT} (422).
* @since 7.0
*/
public ResultMatcher isUnprocessableContent() {
return matcher(HttpStatus.UNPROCESSABLE_CONTENT);
} }
/** /**
* Assert the response status code is {@code HttpStatus.UNPROCESSABLE_ENTITY} (422). * Assert the response status code is {@code HttpStatus.UNPROCESSABLE_ENTITY} (422).
* @deprecated since 7.0 in favor of {@link #isUnprocessableContent()}
*/ */
@Deprecated(since = "7.0")
public ResultMatcher isUnprocessableEntity() { public ResultMatcher isUnprocessableEntity() {
return matcher(HttpStatus.UNPROCESSABLE_ENTITY); return matcher(HttpStatus.UNPROCESSABLE_ENTITY);
} }
@ -538,14 +570,18 @@ public class StatusResultMatchers {
/** /**
* Assert the response status code is {@code HttpStatus.BANDWIDTH_LIMIT_EXCEEDED} (509). * Assert the response status code is {@code HttpStatus.BANDWIDTH_LIMIT_EXCEEDED} (509).
* @deprecated since 7.0, since this is now unassigned
*/ */
@Deprecated(since = "7.0")
public ResultMatcher isBandwidthLimitExceeded() { public ResultMatcher isBandwidthLimitExceeded() {
return matcher(HttpStatus.valueOf(509)); return matcher(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED);
} }
/** /**
* Assert the response status code is {@code HttpStatus.NOT_EXTENDED} (510). * Assert the response status code is {@code HttpStatus.NOT_EXTENDED} (510).
* @deprecated since 7.0, this is now marked as "historic" and not endorsed by a standards body.
*/ */
@Deprecated(since = "7.0")
public ResultMatcher isNotExtended() { public ResultMatcher isNotExtended() {
return matcher(HttpStatus.NOT_EXTENDED); return matcher(HttpStatus.NOT_EXTENDED);
} }

35
spring-test/src/main/kotlin/org/springframework/test/web/servlet/result/StatusResultMatchersDsl.kt

@ -110,6 +110,8 @@ class StatusResultMatchersDsl internal constructor (private val actions: ResultA
/** /**
* @see StatusResultMatchers.isProcessing * @see StatusResultMatchers.isProcessing
*/ */
@Deprecated("Removed from WebDAV specification RFC 4918")
@Suppress("DEPRECATION")
fun isProcessing() { fun isProcessing() {
actions.andExpect(matchers.isProcessing()) actions.andExpect(matchers.isProcessing())
} }
@ -325,9 +327,18 @@ class StatusResultMatchersDsl internal constructor (private val actions: ResultA
actions.andExpect(matchers.isPreconditionFailed()) actions.andExpect(matchers.isPreconditionFailed())
} }
/**
* @see StatusResultMatchers.isContentTooLarge
*/
fun isContentTooLarge() {
actions.andExpect(matchers.isContentTooLarge())
}
/** /**
* @see StatusResultMatchers.isPayloadTooLarge * @see StatusResultMatchers.isPayloadTooLarge
*/ */
@Deprecated("Use Content Too Large instead", replaceWith = ReplaceWith("isContentTooLarge()"))
@Suppress("DEPRECATION")
fun isPayloadTooLarge() { fun isPayloadTooLarge() {
actions.andExpect(matchers.isPayloadTooLarge()) actions.andExpect(matchers.isPayloadTooLarge())
} }
@ -363,13 +374,33 @@ class StatusResultMatchersDsl internal constructor (private val actions: ResultA
/** /**
* @see StatusResultMatchers.isIAmATeapot * @see StatusResultMatchers.isIAmATeapot
*/ */
@Deprecated("Marked as unused in RFC 9110")
@Suppress("DEPRECATION")
fun isIAmATeapot() { fun isIAmATeapot() {
actions.andExpect(matchers.isIAmATeapot()) actions.andExpect(matchers.isIAmATeapot())
} }
/**
* @see StatusResultMatchers.isMisdirectedRequest
* @since 7.0
*/
fun isMisdirectedRequest() {
actions.andExpect(matchers.isMisdirectedRequest())
}
/**
* @see StatusResultMatchers.isUnprocessableContent
* @since 7.0
*/
fun isUnprocessableContent() {
actions.andExpect(matchers.isUnprocessableContent())
}
/** /**
* @see StatusResultMatchers.isUnprocessableEntity * @see StatusResultMatchers.isUnprocessableEntity
*/ */
@Deprecated("Use UnprocessableContent instead.", ReplaceWith("isUnprocessableContent()"))
@Suppress("DEPRECATION")
fun isUnprocessableEntity() { fun isUnprocessableEntity() {
actions.andExpect(matchers.isUnprocessableEntity()) actions.andExpect(matchers.isUnprocessableEntity())
} }
@ -496,6 +527,8 @@ class StatusResultMatchersDsl internal constructor (private val actions: ResultA
/** /**
* @see StatusResultMatchers.isBandwidthLimitExceeded * @see StatusResultMatchers.isBandwidthLimitExceeded
*/ */
@Deprecated("This is now unassigned")
@Suppress("DEPRECATION")
fun isBandwidthLimitExceeded() { fun isBandwidthLimitExceeded() {
actions.andExpect(matchers.isBandwidthLimitExceeded()) actions.andExpect(matchers.isBandwidthLimitExceeded())
} }
@ -503,6 +536,8 @@ class StatusResultMatchersDsl internal constructor (private val actions: ResultA
/** /**
* @see StatusResultMatchers.isNotExtended * @see StatusResultMatchers.isNotExtended
*/ */
@Deprecated("This is marked as 'historic' and is not endorsed by a standards body")
@Suppress("DEPRECATION")
fun isNotExtended() { fun isNotExtended() {
actions.andExpect(matchers.isNotExtended()) actions.andExpect(matchers.isNotExtended())
} }

127
spring-web/src/main/java/org/springframework/http/HttpStatus.java

@ -37,18 +37,20 @@ public enum HttpStatus implements HttpStatusCode {
/** /**
* {@code 100 Continue}. * {@code 100 Continue}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.2.1">HTTP/1.1: Semantics and Content, section 6.2.1</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-100-continue">HTTP Semantics, section 15.2.1</a>
*/ */
CONTINUE(100, Series.INFORMATIONAL, "Continue"), CONTINUE(100, Series.INFORMATIONAL, "Continue"),
/** /**
* {@code 101 Switching Protocols}. * {@code 101 Switching Protocols}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.2.2">HTTP/1.1: Semantics and Content, section 6.2.2</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-101-switching-protocols">HTTP Semantics, section 15.2.2</a>
*/ */
SWITCHING_PROTOCOLS(101, Series.INFORMATIONAL, "Switching Protocols"), SWITCHING_PROTOCOLS(101, Series.INFORMATIONAL, "Switching Protocols"),
/** /**
* {@code 102 Processing}. * {@code 102 Processing}.
* @see <a href="https://tools.ietf.org/html/rfc2518#section-10.1">WebDAV</a> * @see <a href="https://tools.ietf.org/html/rfc2518#section-10.1">WebDAV</a>
* @deprecated since 7.0, removed from <a href="https://datatracker.ietf.org/doc/html/rfc4918#section-21.4">WebDAV specification</a>
*/ */
@Deprecated(since = "7.0")
PROCESSING(102, Series.INFORMATIONAL, "Processing"), PROCESSING(102, Series.INFORMATIONAL, "Processing"),
/** /**
* {@code 103 Early Hints}. * {@code 103 Early Hints}.
@ -61,42 +63,42 @@ public enum HttpStatus implements HttpStatusCode {
/** /**
* {@code 200 OK}. * {@code 200 OK}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.3.1">HTTP/1.1: Semantics and Content, section 6.3.1</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-200-ok">HTTP Semantics, section 15.3.1</a>
*/ */
OK(200, Series.SUCCESSFUL, "OK"), OK(200, Series.SUCCESSFUL, "OK"),
/** /**
* {@code 201 Created}. * {@code 201 Created}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.3.2">HTTP/1.1: Semantics and Content, section 6.3.2</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-201-created">HTTP Semantics, section 15.3.2</a>
*/ */
CREATED(201, Series.SUCCESSFUL, "Created"), CREATED(201, Series.SUCCESSFUL, "Created"),
/** /**
* {@code 202 Accepted}. * {@code 202 Accepted}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.3.3">HTTP/1.1: Semantics and Content, section 6.3.3</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-202-accepted">HTTP Semantics, section 15.3.3</a>
*/ */
ACCEPTED(202, Series.SUCCESSFUL, "Accepted"), ACCEPTED(202, Series.SUCCESSFUL, "Accepted"),
/** /**
* {@code 203 Non-Authoritative Information}. * {@code 203 Non-Authoritative Information}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.3.4">HTTP/1.1: Semantics and Content, section 6.3.4</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-203-non-authoritative-infor">HTTP Semantics, section 15.3.4</a>
*/ */
NON_AUTHORITATIVE_INFORMATION(203, Series.SUCCESSFUL, "Non-Authoritative Information"), NON_AUTHORITATIVE_INFORMATION(203, Series.SUCCESSFUL, "Non-Authoritative Information"),
/** /**
* {@code 204 No Content}. * {@code 204 No Content}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.3.5">HTTP/1.1: Semantics and Content, section 6.3.5</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-204-no-content">HTTP Semantics, section 15.3.5</a>
*/ */
NO_CONTENT(204, Series.SUCCESSFUL, "No Content"), NO_CONTENT(204, Series.SUCCESSFUL, "No Content"),
/** /**
* {@code 205 Reset Content}. * {@code 205 Reset Content}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.3.6">HTTP/1.1: Semantics and Content, section 6.3.6</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-205-reset-content">HTTP Semantics, section 15.3.6</a>
*/ */
RESET_CONTENT(205, Series.SUCCESSFUL, "Reset Content"), RESET_CONTENT(205, Series.SUCCESSFUL, "Reset Content"),
/** /**
* {@code 206 Partial Content}. * {@code 206 Partial Content}.
* @see <a href="https://tools.ietf.org/html/rfc7233#section-4.1">HTTP/1.1: Range Requests, section 4.1</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-206-partial-content">HTTP/1.1: Range Requests, section 4.1</a>
*/ */
PARTIAL_CONTENT(206, Series.SUCCESSFUL, "Partial Content"), PARTIAL_CONTENT(206, Series.SUCCESSFUL, "Partial Content"),
/** /**
* {@code 207 Multi-Status}. * {@code 207 Multi-Status}.
* @see <a href="https://tools.ietf.org/html/rfc4918#section-13">WebDAV</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc4918#section-11.1">WebDAV</a>
*/ */
MULTI_STATUS(207, Series.SUCCESSFUL, "Multi-Status"), MULTI_STATUS(207, Series.SUCCESSFUL, "Multi-Status"),
/** /**
@ -114,37 +116,37 @@ public enum HttpStatus implements HttpStatusCode {
/** /**
* {@code 300 Multiple Choices}. * {@code 300 Multiple Choices}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.4.1">HTTP/1.1: Semantics and Content, section 6.4.1</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-300-multiple-choices">HTTP Semantics, section 15.4.1</a>
*/ */
MULTIPLE_CHOICES(300, Series.REDIRECTION, "Multiple Choices"), MULTIPLE_CHOICES(300, Series.REDIRECTION, "Multiple Choices"),
/** /**
* {@code 301 Moved Permanently}. * {@code 301 Moved Permanently}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.4.2">HTTP/1.1: Semantics and Content, section 6.4.2</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-301-moved-permanently">HTTP Semantics, section 15.4.2</a>
*/ */
MOVED_PERMANENTLY(301, Series.REDIRECTION, "Moved Permanently"), MOVED_PERMANENTLY(301, Series.REDIRECTION, "Moved Permanently"),
/** /**
* {@code 302 Found}. * {@code 302 Found}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.4.3">HTTP/1.1: Semantics and Content, section 6.4.3</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-302-found">HTTP Semantics, section 15.4.3</a>
*/ */
FOUND(302, Series.REDIRECTION, "Found"), FOUND(302, Series.REDIRECTION, "Found"),
/** /**
* {@code 303 See Other}. * {@code 303 See Other}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.4.4">HTTP/1.1: Semantics and Content, section 6.4.4</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-303-see-other">HTTP Semantics, section 15.4.4</a>
*/ */
SEE_OTHER(303, Series.REDIRECTION, "See Other"), SEE_OTHER(303, Series.REDIRECTION, "See Other"),
/** /**
* {@code 304 Not Modified}. * {@code 304 Not Modified}.
* @see <a href="https://tools.ietf.org/html/rfc7232#section-4.1">HTTP/1.1: Conditional Requests, section 4.1</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-304-not-modified">HTTP Semantics, section 15.4.5</a>
*/ */
NOT_MODIFIED(304, Series.REDIRECTION, "Not Modified"), NOT_MODIFIED(304, Series.REDIRECTION, "Not Modified"),
/** /**
* {@code 307 Temporary Redirect}. * {@code 307 Temporary Redirect}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.4.7">HTTP/1.1: Semantics and Content, section 6.4.7</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-307-temporary-redirect">HTTP Semantics, section 15.4.8</a>
*/ */
TEMPORARY_REDIRECT(307, Series.REDIRECTION, "Temporary Redirect"), TEMPORARY_REDIRECT(307, Series.REDIRECTION, "Temporary Redirect"),
/** /**
* {@code 308 Permanent Redirect}. * {@code 308 Permanent Redirect}.
* @see <a href="https://tools.ietf.org/html/rfc7238">RFC 7238</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-308-permanent-redirect">HTTP Semantics, section 15.4.9</a>
*/ */
PERMANENT_REDIRECT(308, Series.REDIRECTION, "Permanent Redirect"), PERMANENT_REDIRECT(308, Series.REDIRECTION, "Permanent Redirect"),
@ -152,70 +154,70 @@ public enum HttpStatus implements HttpStatusCode {
/** /**
* {@code 400 Bad Request}. * {@code 400 Bad Request}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.1">HTTP/1.1: Semantics and Content, section 6.5.1</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-400-bad-request">HTTP Semantics, section 15.5.1</a>
*/ */
BAD_REQUEST(400, Series.CLIENT_ERROR, "Bad Request"), BAD_REQUEST(400, Series.CLIENT_ERROR, "Bad Request"),
/** /**
* {@code 401 Unauthorized}. * {@code 401 Unauthorized}.
* @see <a href="https://tools.ietf.org/html/rfc7235#section-3.1">HTTP/1.1: Authentication, section 3.1</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-401-unauthorized">HTTP Semantics, section 15.5.2</a>
*/ */
UNAUTHORIZED(401, Series.CLIENT_ERROR, "Unauthorized"), UNAUTHORIZED(401, Series.CLIENT_ERROR, "Unauthorized"),
/** /**
* {@code 402 Payment Required}. * {@code 402 Payment Required}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.2">HTTP/1.1: Semantics and Content, section 6.5.2</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-402-payment-required">HTTP Semantics, section 15.5.3</a>
*/ */
PAYMENT_REQUIRED(402, Series.CLIENT_ERROR, "Payment Required"), PAYMENT_REQUIRED(402, Series.CLIENT_ERROR, "Payment Required"),
/** /**
* {@code 403 Forbidden}. * {@code 403 Forbidden}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.3">HTTP/1.1: Semantics and Content, section 6.5.3</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-403-forbidden">HTTP Semantics, section 15.5.4</a>
*/ */
FORBIDDEN(403, Series.CLIENT_ERROR, "Forbidden"), FORBIDDEN(403, Series.CLIENT_ERROR, "Forbidden"),
/** /**
* {@code 404 Not Found}. * {@code 404 Not Found}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.4">HTTP/1.1: Semantics and Content, section 6.5.4</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-404-not-found">HTTP Semantics, section 15.5.5</a>
*/ */
NOT_FOUND(404, Series.CLIENT_ERROR, "Not Found"), NOT_FOUND(404, Series.CLIENT_ERROR, "Not Found"),
/** /**
* {@code 405 Method Not Allowed}. * {@code 405 Method Not Allowed}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.5">HTTP/1.1: Semantics and Content, section 6.5.5</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-405-method-not-allowed">HTTP Semantics, section 15.5.6</a>
*/ */
METHOD_NOT_ALLOWED(405, Series.CLIENT_ERROR, "Method Not Allowed"), METHOD_NOT_ALLOWED(405, Series.CLIENT_ERROR, "Method Not Allowed"),
/** /**
* {@code 406 Not Acceptable}. * {@code 406 Not Acceptable}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.6">HTTP/1.1: Semantics and Content, section 6.5.6</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-406-not-acceptable">HTTP Semantics, section 15.5.7</a>
*/ */
NOT_ACCEPTABLE(406, Series.CLIENT_ERROR, "Not Acceptable"), NOT_ACCEPTABLE(406, Series.CLIENT_ERROR, "Not Acceptable"),
/** /**
* {@code 407 Proxy Authentication Required}. * {@code 407 Proxy Authentication Required}.
* @see <a href="https://tools.ietf.org/html/rfc7235#section-3.2">HTTP/1.1: Authentication, section 3.2</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-407-proxy-authentication-re">HTTP Semantics, section 15.5.8</a>
*/ */
PROXY_AUTHENTICATION_REQUIRED(407, Series.CLIENT_ERROR, "Proxy Authentication Required"), PROXY_AUTHENTICATION_REQUIRED(407, Series.CLIENT_ERROR, "Proxy Authentication Required"),
/** /**
* {@code 408 Request Timeout}. * {@code 408 Request Timeout}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.7">HTTP/1.1: Semantics and Content, section 6.5.7</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-408-request-timeout">HTTP Semantics, section 15.5.9</a>
*/ */
REQUEST_TIMEOUT(408, Series.CLIENT_ERROR, "Request Timeout"), REQUEST_TIMEOUT(408, Series.CLIENT_ERROR, "Request Timeout"),
/** /**
* {@code 409 Conflict}. * {@code 409 Conflict}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.8">HTTP/1.1: Semantics and Content, section 6.5.8</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-409-conflict">HTTP Semantics, section 15.5.10</a>
*/ */
CONFLICT(409, Series.CLIENT_ERROR, "Conflict"), CONFLICT(409, Series.CLIENT_ERROR, "Conflict"),
/** /**
* {@code 410 Gone}. * {@code 410 Gone}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.9"> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-410-gone">
* HTTP/1.1: Semantics and Content, section 6.5.9</a> * HTTP Semantics, section 15.5.11</a>
*/ */
GONE(410, Series.CLIENT_ERROR, "Gone"), GONE(410, Series.CLIENT_ERROR, "Gone"),
/** /**
* {@code 411 Length Required}. * {@code 411 Length Required}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.10"> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-411-length-required">
* HTTP/1.1: Semantics and Content, section 6.5.10</a> * HTTP Semantics, section 15.5.12</a>
*/ */
LENGTH_REQUIRED(411, Series.CLIENT_ERROR, "Length Required"), LENGTH_REQUIRED(411, Series.CLIENT_ERROR, "Length Required"),
/** /**
* {@code 412 Precondition failed}. * {@code 412 Precondition failed}.
* @see <a href="https://tools.ietf.org/html/rfc7232#section-4.2"> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-412-precondition-failed">
* HTTP/1.1: Conditional Requests, section 4.2</a> * HTTP Semantics, section 15.5.13</a>
*/ */
PRECONDITION_FAILED(412, Series.CLIENT_ERROR, "Precondition Failed"), PRECONDITION_FAILED(412, Series.CLIENT_ERROR, "Precondition Failed"),
/** /**
@ -223,41 +225,67 @@ public enum HttpStatus implements HttpStatusCode {
* @since 4.1 * @since 4.1
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.11"> * @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.11">
* HTTP/1.1: Semantics and Content, section 6.5.11</a> * HTTP/1.1: Semantics and Content, section 6.5.11</a>
* @deprecated since 7.0 in favor of {@link #CONTENT_TOO_LARGE}
*/ */
@Deprecated(since = "7.0")
PAYLOAD_TOO_LARGE(413, Series.CLIENT_ERROR, "Payload Too Large"), PAYLOAD_TOO_LARGE(413, Series.CLIENT_ERROR, "Payload Too Large"),
/**
* {@code 413 Content Too Large}.
* @since 7.0
* @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-413-content-too-large">
* HTTP Semantics, section 15.5.14</a>
*/
CONTENT_TOO_LARGE(413, Series.CLIENT_ERROR, "Content Too Large"),
/** /**
* {@code 414 URI Too Long}. * {@code 414 URI Too Long}.
* @since 4.1 * @since 4.1
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.12"> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-414-uri-too-long">
* HTTP/1.1: Semantics and Content, section 6.5.12</a> * HTTP Semantics, section 15.5.15</a>
*/ */
URI_TOO_LONG(414, Series.CLIENT_ERROR, "URI Too Long"), URI_TOO_LONG(414, Series.CLIENT_ERROR, "URI Too Long"),
/** /**
* {@code 415 Unsupported Media Type}. * {@code 415 Unsupported Media Type}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.13"> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-415-unsupported-media-type">
* HTTP/1.1: Semantics and Content, section 6.5.13</a> * HTTP Semantics, section 15.5.16</a>
*/ */
UNSUPPORTED_MEDIA_TYPE(415, Series.CLIENT_ERROR, "Unsupported Media Type"), UNSUPPORTED_MEDIA_TYPE(415, Series.CLIENT_ERROR, "Unsupported Media Type"),
/** /**
* {@code 416 Requested Range Not Satisfiable}. * {@code 416 Requested Range Not Satisfiable}.
* @see <a href="https://tools.ietf.org/html/rfc7233#section-4.4">HTTP/1.1: Range Requests, section 4.4</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-416-range-not-satisfiable">
* HTTP Semantics, section 15.5.17</a>
*/ */
REQUESTED_RANGE_NOT_SATISFIABLE(416, Series.CLIENT_ERROR, "Requested range not satisfiable"), REQUESTED_RANGE_NOT_SATISFIABLE(416, Series.CLIENT_ERROR, "Requested range not satisfiable"),
/** /**
* {@code 417 Expectation Failed}. * {@code 417 Expectation Failed}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.14"> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-417-expectation-failed">
* HTTP/1.1: Semantics and Content, section 6.5.14</a> * HTTP Semantics, section 15.5.18</a>
*/ */
EXPECTATION_FAILED(417, Series.CLIENT_ERROR, "Expectation Failed"), EXPECTATION_FAILED(417, Series.CLIENT_ERROR, "Expectation Failed"),
/** /**
* {@code 418 I'm a teapot}. * {@code 418 I'm a teapot}.
* @see <a href="https://tools.ietf.org/html/rfc2324#section-2.3.2">HTCPCP/1.0</a> * @see <a href="https://tools.ietf.org/html/rfc2324#section-2.3.2">HTCPCP/1.0</a>
* @deprecated since 7.0, marked as unused in RFC 9110
*/ */
@Deprecated(since = "7.0")
I_AM_A_TEAPOT(418, Series.CLIENT_ERROR, "I'm a teapot"), I_AM_A_TEAPOT(418, Series.CLIENT_ERROR, "I'm a teapot"),
/**
* {@code 421 Misdirected Request}.
* @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-421-misdirected-request">
* HTTP Semantics, section 15.5.20</a>
*/
MISDIRECTED_REQUEST(421, Series.CLIENT_ERROR, "Misdirected Request"),
/**
* {@code 422 Unprocessable Content}.
* @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-422-unprocessable-content">
* HTTP Semantics, section 15.5.21</a>
*/
UNPROCESSABLE_CONTENT(422, Series.CLIENT_ERROR, "Unprocessable Content"),
/** /**
* {@code 422 Unprocessable Entity}. * {@code 422 Unprocessable Entity}.
* @see <a href="https://tools.ietf.org/html/rfc4918#section-11.2">WebDAV</a> * @see <a href="https://tools.ietf.org/html/rfc4918#section-11.2">WebDAV</a>
* @deprecated since 7.0 in favor of {@link #UNPROCESSABLE_CONTENT}
*/ */
@Deprecated(since = "7.0")
UNPROCESSABLE_ENTITY(422, Series.CLIENT_ERROR, "Unprocessable Entity"), UNPROCESSABLE_ENTITY(422, Series.CLIENT_ERROR, "Unprocessable Entity"),
/** /**
* {@code 423 Locked}. * {@code 423 Locked}.
@ -277,7 +305,8 @@ public enum HttpStatus implements HttpStatusCode {
TOO_EARLY(425, Series.CLIENT_ERROR, "Too Early"), TOO_EARLY(425, Series.CLIENT_ERROR, "Too Early"),
/** /**
* {@code 426 Upgrade Required}. * {@code 426 Upgrade Required}.
* @see <a href="https://tools.ietf.org/html/rfc2817#section-6">Upgrading to TLS Within HTTP/1.1</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-426-upgrade-required">
* HTTP Semantics, section 15.5.22</a>
*/ */
UPGRADE_REQUIRED(426, Series.CLIENT_ERROR, "Upgrade Required"), UPGRADE_REQUIRED(426, Series.CLIENT_ERROR, "Upgrade Required"),
/** /**
@ -307,32 +336,32 @@ public enum HttpStatus implements HttpStatusCode {
/** /**
* {@code 500 Internal Server Error}. * {@code 500 Internal Server Error}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.6.1">HTTP/1.1: Semantics and Content, section 6.6.1</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#section-15.6.1">HTTP Semantics, section 15.6.1</a>
*/ */
INTERNAL_SERVER_ERROR(500, Series.SERVER_ERROR, "Internal Server Error"), INTERNAL_SERVER_ERROR(500, Series.SERVER_ERROR, "Internal Server Error"),
/** /**
* {@code 501 Not Implemented}. * {@code 501 Not Implemented}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.6.2">HTTP/1.1: Semantics and Content, section 6.6.2</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-501-not-implemented">HTTP Semantics, section 15.6.2</a>
*/ */
NOT_IMPLEMENTED(501, Series.SERVER_ERROR, "Not Implemented"), NOT_IMPLEMENTED(501, Series.SERVER_ERROR, "Not Implemented"),
/** /**
* {@code 502 Bad Gateway}. * {@code 502 Bad Gateway}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.6.3">HTTP/1.1: Semantics and Content, section 6.6.3</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-502-bad-gateway">HTTP Semantics, section 15.6.3</a>
*/ */
BAD_GATEWAY(502, Series.SERVER_ERROR, "Bad Gateway"), BAD_GATEWAY(502, Series.SERVER_ERROR, "Bad Gateway"),
/** /**
* {@code 503 Service Unavailable}. * {@code 503 Service Unavailable}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.6.4">HTTP/1.1: Semantics and Content, section 6.6.4</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-503-service-unavailable">HTTP Semantics, section 15.6.4</a>
*/ */
SERVICE_UNAVAILABLE(503, Series.SERVER_ERROR, "Service Unavailable"), SERVICE_UNAVAILABLE(503, Series.SERVER_ERROR, "Service Unavailable"),
/** /**
* {@code 504 Gateway Timeout}. * {@code 504 Gateway Timeout}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.6.5">HTTP/1.1: Semantics and Content, section 6.6.5</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-504-gateway-timeout">HTTP Semantics, section 15.6.5</a>
*/ */
GATEWAY_TIMEOUT(504, Series.SERVER_ERROR, "Gateway Timeout"), GATEWAY_TIMEOUT(504, Series.SERVER_ERROR, "Gateway Timeout"),
/** /**
* {@code 505 HTTP Version Not Supported}. * {@code 505 HTTP Version Not Supported}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.6.6">HTTP/1.1: Semantics and Content, section 6.6.6</a> * @see <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-505-http-version-not-suppor">HTTP Semantics, section 15.6.6</a>
*/ */
HTTP_VERSION_NOT_SUPPORTED(505, Series.SERVER_ERROR, "HTTP Version not supported"), HTTP_VERSION_NOT_SUPPORTED(505, Series.SERVER_ERROR, "HTTP Version not supported"),
/** /**
@ -352,12 +381,16 @@ public enum HttpStatus implements HttpStatusCode {
LOOP_DETECTED(508, Series.SERVER_ERROR, "Loop Detected"), LOOP_DETECTED(508, Series.SERVER_ERROR, "Loop Detected"),
/** /**
* {@code 509 Bandwidth Limit Exceeded} * {@code 509 Bandwidth Limit Exceeded}
* @deprecated since 7.0, this is now unassigned
*/ */
@Deprecated(since = "7.0")
BANDWIDTH_LIMIT_EXCEEDED(509, Series.SERVER_ERROR, "Bandwidth Limit Exceeded"), BANDWIDTH_LIMIT_EXCEEDED(509, Series.SERVER_ERROR, "Bandwidth Limit Exceeded"),
/** /**
* {@code 510 Not Extended} * {@code 510 Not Extended}
* @see <a href="https://tools.ietf.org/html/rfc2774#section-7">HTTP Extension Framework</a> * @see <a href="https://tools.ietf.org/html/rfc2774#section-7">HTTP Extension Framework</a>
* @deprecated since 7.0, this is now marked as "historic" and not endorsed by a standards body.
*/ */
@Deprecated(since = "7.0")
NOT_EXTENDED(510, Series.SERVER_ERROR, "Not Extended"), NOT_EXTENDED(510, Series.SERVER_ERROR, "Not Extended"),
/** /**
* {@code 511 Network Authentication Required}. * {@code 511 Network Authentication Required}.

12
spring-web/src/main/java/org/springframework/http/ResponseEntity.java

@ -362,12 +362,24 @@ public class ResponseEntity<T> extends HttpEntity<T> {
return status(HttpStatus.NOT_FOUND); return status(HttpStatus.NOT_FOUND);
} }
/**
* Create a builder with an
* {@linkplain HttpStatus#UNPROCESSABLE_CONTENT UNPROCESSABLE_CONTENT} status.
* @return the created builder
* @since 7.0
*/
public static BodyBuilder unprocessableContent() {
return status(HttpStatus.UNPROCESSABLE_CONTENT);
}
/** /**
* Create a builder with an * Create a builder with an
* {@linkplain HttpStatus#UNPROCESSABLE_ENTITY UNPROCESSABLE_ENTITY} status. * {@linkplain HttpStatus#UNPROCESSABLE_ENTITY UNPROCESSABLE_ENTITY} status.
* @return the created builder * @return the created builder
* @since 4.1.3 * @since 4.1.3
* @deprecated since 7.0 in favor of {@link #unprocessableContent()}
*/ */
@Deprecated(since = "7.0")
public static BodyBuilder unprocessableEntity() { public static BodyBuilder unprocessableEntity() {
return status(HttpStatus.UNPROCESSABLE_ENTITY); return status(HttpStatus.UNPROCESSABLE_ENTITY);
} }

23
spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java

@ -133,6 +133,9 @@ public class HttpClientErrorException extends HttpStatusCodeException {
case UNPROCESSABLE_ENTITY -> message != null ? case UNPROCESSABLE_ENTITY -> message != null ?
new UnprocessableEntity(message, statusText, headers, body, charset) : new UnprocessableEntity(message, statusText, headers, body, charset) :
new UnprocessableEntity(statusText, headers, body, charset); new UnprocessableEntity(statusText, headers, body, charset);
case UNPROCESSABLE_CONTENT -> message != null ?
new UnprocessableContent(message, statusText, headers, body, charset) :
new UnprocessableContent(statusText, headers, body, charset);
default -> message != null ? default -> message != null ?
new HttpClientErrorException(message, statusCode, statusText, headers, body, charset) : new HttpClientErrorException(message, statusCode, statusText, headers, body, charset) :
new HttpClientErrorException(statusCode, statusText, headers, body, charset); new HttpClientErrorException(statusCode, statusText, headers, body, charset);
@ -307,10 +310,30 @@ public class HttpClientErrorException extends HttpStatusCodeException {
} }
} }
/**
* {@link HttpClientErrorException} for status HTTP 422 Unprocessable Content.
* @since 7.0
*/
@SuppressWarnings("serial")
public static final class UnprocessableContent extends HttpClientErrorException {
private UnprocessableContent(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
super(HttpStatus.UNPROCESSABLE_CONTENT, statusText, headers, body, charset);
}
private UnprocessableContent(String message, String statusText,
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
super(message, HttpStatus.UNPROCESSABLE_CONTENT, statusText, headers, body, charset);
}
}
/** /**
* {@link HttpClientErrorException} for status HTTP 422 Unprocessable Entity. * {@link HttpClientErrorException} for status HTTP 422 Unprocessable Entity.
* @since 5.1 * @since 5.1
* @deprecated since 7.0 in favor of {@link UnprocessableContent}
*/ */
@Deprecated(since = "7.0")
@SuppressWarnings("serial") @SuppressWarnings("serial")
public static final class UnprocessableEntity extends HttpClientErrorException { public static final class UnprocessableEntity extends HttpClientErrorException {

4
spring-web/src/main/java/org/springframework/web/multipart/MaxUploadSizeExceededException.java

@ -35,7 +35,7 @@ import org.springframework.web.ErrorResponse;
public class MaxUploadSizeExceededException extends MultipartException implements ErrorResponse { public class MaxUploadSizeExceededException extends MultipartException implements ErrorResponse {
private final ProblemDetail body = private final ProblemDetail body =
ProblemDetail.forStatusAndDetail(HttpStatus.PAYLOAD_TOO_LARGE, "Maximum upload size exceeded"); ProblemDetail.forStatusAndDetail(HttpStatus.CONTENT_TOO_LARGE, "Maximum upload size exceeded");
private final long maxUploadSize; private final long maxUploadSize;
@ -71,7 +71,7 @@ public class MaxUploadSizeExceededException extends MultipartException implement
@Override @Override
public HttpStatusCode getStatusCode() { public HttpStatusCode getStatusCode() {
return HttpStatus.PAYLOAD_TOO_LARGE; return HttpStatus.CONTENT_TOO_LARGE;
} }
@Override @Override

37
spring-web/src/main/java/org/springframework/web/server/ContentTooLargeException.java

@ -0,0 +1,37 @@
/*
* Copyright 2002-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.server;
import org.jspecify.annotations.Nullable;
import org.springframework.http.HttpStatus;
/**
* Exception for errors that fit response status 413 (Content too large) for use in
* Spring Web applications.
*
* @author Brian Clozel
* @since 7.0
*/
@SuppressWarnings("serial")
public class ContentTooLargeException extends ResponseStatusException {
public ContentTooLargeException(@Nullable Throwable cause) {
super(HttpStatus.CONTENT_TOO_LARGE, null, cause);
}
}

2
spring-web/src/main/java/org/springframework/web/server/PayloadTooLargeException.java

@ -27,8 +27,10 @@ import org.springframework.http.HttpStatus;
* *
* @author Kim Bosung * @author Kim Bosung
* @since 6.2 * @since 6.2
* @deprecated since 7.0 in favor of {@link ContentTooLargeException}
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
@Deprecated(since = "7.0")
public class PayloadTooLargeException extends ResponseStatusException { public class PayloadTooLargeException extends ResponseStatusException {
public PayloadTooLargeException(@Nullable Throwable cause) { public PayloadTooLargeException(@Nullable Throwable cause) {

146
spring-web/src/test/java/org/springframework/http/HttpStatusTests.java

@ -16,12 +16,15 @@
package org.springframework.http; package org.springframework.http;
import java.util.LinkedHashMap; import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
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.assertThat;
/** /**
@ -29,86 +32,89 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
class HttpStatusTests { class HttpStatusTests {
private final Map<Integer, String> statusCodes = new LinkedHashMap<>(); private final MultiValueMap<Integer, String> statusCodes = new LinkedMultiValueMap<>();
@BeforeEach @BeforeEach
void createStatusCodes() { void createStatusCodes() {
statusCodes.put(100, "CONTINUE"); statusCodes.add(100, "CONTINUE");
statusCodes.put(101, "SWITCHING_PROTOCOLS"); statusCodes.add(101, "SWITCHING_PROTOCOLS");
statusCodes.put(102, "PROCESSING"); statusCodes.add(102, "PROCESSING");
statusCodes.put(103, "EARLY_HINTS"); statusCodes.add(103, "EARLY_HINTS");
statusCodes.put(200, "OK"); statusCodes.add(200, "OK");
statusCodes.put(201, "CREATED"); statusCodes.add(201, "CREATED");
statusCodes.put(202, "ACCEPTED"); statusCodes.add(202, "ACCEPTED");
statusCodes.put(203, "NON_AUTHORITATIVE_INFORMATION"); statusCodes.add(203, "NON_AUTHORITATIVE_INFORMATION");
statusCodes.put(204, "NO_CONTENT"); statusCodes.add(204, "NO_CONTENT");
statusCodes.put(205, "RESET_CONTENT"); statusCodes.add(205, "RESET_CONTENT");
statusCodes.put(206, "PARTIAL_CONTENT"); statusCodes.add(206, "PARTIAL_CONTENT");
statusCodes.put(207, "MULTI_STATUS"); statusCodes.add(207, "MULTI_STATUS");
statusCodes.put(208, "ALREADY_REPORTED"); statusCodes.add(208, "ALREADY_REPORTED");
statusCodes.put(226, "IM_USED"); statusCodes.add(226, "IM_USED");
statusCodes.put(300, "MULTIPLE_CHOICES"); statusCodes.add(300, "MULTIPLE_CHOICES");
statusCodes.put(301, "MOVED_PERMANENTLY"); statusCodes.add(301, "MOVED_PERMANENTLY");
statusCodes.put(302, "FOUND"); statusCodes.add(302, "FOUND");
statusCodes.put(303, "SEE_OTHER"); statusCodes.add(303, "SEE_OTHER");
statusCodes.put(304, "NOT_MODIFIED"); statusCodes.add(304, "NOT_MODIFIED");
statusCodes.put(307, "TEMPORARY_REDIRECT"); statusCodes.add(307, "TEMPORARY_REDIRECT");
statusCodes.put(308, "PERMANENT_REDIRECT"); statusCodes.add(308, "PERMANENT_REDIRECT");
statusCodes.put(400, "BAD_REQUEST"); statusCodes.add(400, "BAD_REQUEST");
statusCodes.put(401, "UNAUTHORIZED"); statusCodes.add(401, "UNAUTHORIZED");
statusCodes.put(402, "PAYMENT_REQUIRED"); statusCodes.add(402, "PAYMENT_REQUIRED");
statusCodes.put(403, "FORBIDDEN"); statusCodes.add(403, "FORBIDDEN");
statusCodes.put(404, "NOT_FOUND"); statusCodes.add(404, "NOT_FOUND");
statusCodes.put(405, "METHOD_NOT_ALLOWED"); statusCodes.add(405, "METHOD_NOT_ALLOWED");
statusCodes.put(406, "NOT_ACCEPTABLE"); statusCodes.add(406, "NOT_ACCEPTABLE");
statusCodes.put(407, "PROXY_AUTHENTICATION_REQUIRED"); statusCodes.add(407, "PROXY_AUTHENTICATION_REQUIRED");
statusCodes.put(408, "REQUEST_TIMEOUT"); statusCodes.add(408, "REQUEST_TIMEOUT");
statusCodes.put(409, "CONFLICT"); statusCodes.add(409, "CONFLICT");
statusCodes.put(410, "GONE"); statusCodes.add(410, "GONE");
statusCodes.put(411, "LENGTH_REQUIRED"); statusCodes.add(411, "LENGTH_REQUIRED");
statusCodes.put(412, "PRECONDITION_FAILED"); statusCodes.add(412, "PRECONDITION_FAILED");
statusCodes.put(413, "PAYLOAD_TOO_LARGE"); statusCodes.add(413, "PAYLOAD_TOO_LARGE");
statusCodes.put(414, "URI_TOO_LONG"); statusCodes.add(413, "CONTENT_TOO_LARGE");
statusCodes.put(415, "UNSUPPORTED_MEDIA_TYPE"); statusCodes.add(414, "URI_TOO_LONG");
statusCodes.put(416, "REQUESTED_RANGE_NOT_SATISFIABLE"); statusCodes.add(415, "UNSUPPORTED_MEDIA_TYPE");
statusCodes.put(417, "EXPECTATION_FAILED"); statusCodes.add(416, "REQUESTED_RANGE_NOT_SATISFIABLE");
statusCodes.put(418, "I_AM_A_TEAPOT"); statusCodes.add(417, "EXPECTATION_FAILED");
statusCodes.put(422, "UNPROCESSABLE_ENTITY"); statusCodes.add(418, "I_AM_A_TEAPOT");
statusCodes.put(423, "LOCKED"); statusCodes.add(421, "MISDIRECTED_REQUEST");
statusCodes.put(424, "FAILED_DEPENDENCY"); statusCodes.add(422, "UNPROCESSABLE_CONTENT");
statusCodes.put(425, "TOO_EARLY"); statusCodes.add(422, "UNPROCESSABLE_ENTITY");
statusCodes.put(426, "UPGRADE_REQUIRED"); statusCodes.add(423, "LOCKED");
statusCodes.put(428, "PRECONDITION_REQUIRED"); statusCodes.add(424, "FAILED_DEPENDENCY");
statusCodes.put(429, "TOO_MANY_REQUESTS"); statusCodes.add(425, "TOO_EARLY");
statusCodes.put(431, "REQUEST_HEADER_FIELDS_TOO_LARGE"); statusCodes.add(426, "UPGRADE_REQUIRED");
statusCodes.put(451, "UNAVAILABLE_FOR_LEGAL_REASONS"); statusCodes.add(428, "PRECONDITION_REQUIRED");
statusCodes.add(429, "TOO_MANY_REQUESTS");
statusCodes.put(500, "INTERNAL_SERVER_ERROR"); statusCodes.add(431, "REQUEST_HEADER_FIELDS_TOO_LARGE");
statusCodes.put(501, "NOT_IMPLEMENTED"); statusCodes.add(451, "UNAVAILABLE_FOR_LEGAL_REASONS");
statusCodes.put(502, "BAD_GATEWAY");
statusCodes.put(503, "SERVICE_UNAVAILABLE"); statusCodes.add(500, "INTERNAL_SERVER_ERROR");
statusCodes.put(504, "GATEWAY_TIMEOUT"); statusCodes.add(501, "NOT_IMPLEMENTED");
statusCodes.put(505, "HTTP_VERSION_NOT_SUPPORTED"); statusCodes.add(502, "BAD_GATEWAY");
statusCodes.put(506, "VARIANT_ALSO_NEGOTIATES"); statusCodes.add(503, "SERVICE_UNAVAILABLE");
statusCodes.put(507, "INSUFFICIENT_STORAGE"); statusCodes.add(504, "GATEWAY_TIMEOUT");
statusCodes.put(508, "LOOP_DETECTED"); statusCodes.add(505, "HTTP_VERSION_NOT_SUPPORTED");
statusCodes.put(509, "BANDWIDTH_LIMIT_EXCEEDED"); statusCodes.add(506, "VARIANT_ALSO_NEGOTIATES");
statusCodes.put(510, "NOT_EXTENDED"); statusCodes.add(507, "INSUFFICIENT_STORAGE");
statusCodes.put(511, "NETWORK_AUTHENTICATION_REQUIRED"); statusCodes.add(508, "LOOP_DETECTED");
statusCodes.add(509, "BANDWIDTH_LIMIT_EXCEEDED");
statusCodes.add(510, "NOT_EXTENDED");
statusCodes.add(511, "NETWORK_AUTHENTICATION_REQUIRED");
} }
@Test @Test
void fromMapToEnum() { void fromMapToEnum() {
for (Map.Entry<Integer, String> entry : statusCodes.entrySet()) { for (Map.Entry<Integer, List<String>> entry : statusCodes.entrySet()) {
int value = entry.getKey(); int value = entry.getKey();
HttpStatus status = HttpStatus.valueOf(value); HttpStatus status = HttpStatus.valueOf(value);
assertThat(status.value()).as("Invalid value").isEqualTo(value); assertThat(status.value()).as("Invalid value").isEqualTo(value);
assertThat(status.name()).as("Invalid name for [" + value + "]").isEqualTo(entry.getValue()); assertThat(entry.getValue()).as("Invalid name for [" + value + "]").contains(status.name());
} }
} }
@ -117,7 +123,7 @@ class HttpStatusTests {
for (HttpStatus status : HttpStatus.values()) { for (HttpStatus status : HttpStatus.values()) {
int code = status.value(); int code = status.value();
assertThat(statusCodes).as("Map has no value for [" + code + "]").containsKey(code); assertThat(statusCodes).as("Map has no value for [" + code + "]").containsKey(code);
assertThat(status.name()).as("Invalid name for [" + code + "]").isEqualTo(statusCodes.get(code)); assertThat(statusCodes.get(code)).as("Invalid name for [" + code + "]").contains(status.name());
} }
} }

10
spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java

@ -166,6 +166,16 @@ class ResponseEntityTests {
} }
@Test @Test
void unprocessableContent() {
ResponseEntity<String> responseEntity = ResponseEntity.unprocessableContent().body("error");
assertThat(responseEntity).isNotNull();
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.UNPROCESSABLE_CONTENT);
assertThat(responseEntity.getBody()).isEqualTo("error");
}
@Test
@SuppressWarnings("deprecate")
void unprocessableEntity() { void unprocessableEntity() {
ResponseEntity<String> responseEntity = ResponseEntity.unprocessableEntity().body("error"); ResponseEntity<String> responseEntity = ResponseEntity.unprocessableEntity().body("error");

18
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java

@ -464,11 +464,29 @@ public class WebClientResponseException extends WebClientException {
} }
} }
/**
* {@link WebClientResponseException} for status HTTP 422 Unprocessable Content.
* @since 7.0
*/
@SuppressWarnings("serial")
public static class UnprocessableContent extends WebClientResponseException {
UnprocessableContent(
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.UNPROCESSABLE_CONTENT.value(), statusText, headers, body, charset, request);
}
}
/** /**
* {@link WebClientResponseException} for status HTTP 422 Unprocessable Entity. * {@link WebClientResponseException} for status HTTP 422 Unprocessable Entity.
* @since 5.1 * @since 5.1
* @deprecated since 7.0 in favor of {@link UnprocessableContent}
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
@Deprecated(since = "7.0")
public static class UnprocessableEntity extends WebClientResponseException { public static class UnprocessableEntity extends WebClientResponseException {
UnprocessableEntity( UnprocessableEntity(

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

@ -212,11 +212,22 @@ public interface ServerResponse {
return status(HttpStatus.NOT_FOUND); return status(HttpStatus.NOT_FOUND);
} }
/**
* Create a builder with an
* {@linkplain HttpStatus#UNPROCESSABLE_CONTENT 422 Unprocessable Content} status.
* @return the created builder
*/
static BodyBuilder unprocessableContent() {
return status(HttpStatus.UNPROCESSABLE_CONTENT);
}
/** /**
* Create a builder with an * Create a builder with an
* {@linkplain HttpStatus#UNPROCESSABLE_ENTITY 422 Unprocessable Entity} status. * {@linkplain HttpStatus#UNPROCESSABLE_ENTITY 422 Unprocessable Entity} status.
* @return the created builder * @return the created builder
* @deprecated since 7.0 in favor of {@link #unprocessableContent()}
*/ */
@Deprecated(since = "7.0")
static BodyBuilder unprocessableEntity() { static BodyBuilder unprocessableEntity() {
return status(HttpStatus.UNPROCESSABLE_ENTITY); return status(HttpStatus.UNPROCESSABLE_ENTITY);
} }

4
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageReaderArgumentResolver.java

@ -52,7 +52,7 @@ import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.bind.support.WebExchangeDataBinder; import org.springframework.web.bind.support.WebExchangeDataBinder;
import org.springframework.web.reactive.BindingContext; import org.springframework.web.reactive.BindingContext;
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport; import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport;
import org.springframework.web.server.PayloadTooLargeException; import org.springframework.web.server.ContentTooLargeException;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebInputException; import org.springframework.web.server.ServerWebInputException;
@ -236,7 +236,7 @@ public abstract class AbstractMessageReaderArgumentResolver extends HandlerMetho
private Throwable handleReadError(MethodParameter parameter, Throwable ex) { private Throwable handleReadError(MethodParameter parameter, Throwable ex) {
if (ex instanceof DataBufferLimitException) { if (ex instanceof DataBufferLimitException) {
return new PayloadTooLargeException(ex); return new ContentTooLargeException(ex);
} }
if (ex instanceof DecodingException) { if (ex instanceof DecodingException) {
return new ServerWebInputException("Failed to read HTTP message", parameter, ex); return new ServerWebInputException("Failed to read HTTP message", parameter, ex);

7
spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDsl.kt

@ -807,9 +807,16 @@ class CoRouterFunctionDsl internal constructor (private val init: (CoRouterFunct
*/ */
fun notFound() = ServerResponse.notFound() fun notFound() = ServerResponse.notFound()
/**
* @see ServerResponse.unprocessableContent
*/
fun unprocessableContent() = ServerResponse.unprocessableContent()
/** /**
* @see ServerResponse.unprocessableEntity * @see ServerResponse.unprocessableEntity
*/ */
@Deprecated("Use unprocessable content instead.", ReplaceWith("unprocessableContent()"))
@Suppress("DEPRECATION")
fun unprocessableEntity() = ServerResponse.unprocessableEntity() fun unprocessableEntity() = ServerResponse.unprocessableEntity()
/** /**

11
spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt

@ -870,12 +870,23 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs
fun notFound(): ServerResponse.HeadersBuilder<*> = fun notFound(): ServerResponse.HeadersBuilder<*> =
ServerResponse.notFound() ServerResponse.notFound()
/**
* Create a builder with an
* [422 Unprocessable Content][HttpStatus.UNPROCESSABLE_CONTENT] status.
* @return the created builder
* @since 7.0
*/
fun unprocessableContent(): ServerResponse.BodyBuilder =
ServerResponse.unprocessableContent()
/** /**
* Create a builder with an * Create a builder with an
* [422 Unprocessable Entity][HttpStatus.UNPROCESSABLE_ENTITY] status. * [422 Unprocessable Entity][HttpStatus.UNPROCESSABLE_ENTITY] status.
* @return the created builder * @return the created builder
* @since 5.1 * @since 5.1
*/ */
@Deprecated("Use unprocessable content instead.", ReplaceWith("unprocessableContent()"))
@Suppress("DEPRECATION")
fun unprocessableEntity(): ServerResponse.BodyBuilder = fun unprocessableEntity(): ServerResponse.BodyBuilder =
ServerResponse.unprocessableEntity() ServerResponse.unprocessableEntity()

4
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageReaderArgumentResolverTests.java

@ -52,7 +52,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.method.HandlerMethod; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.BindingContext; import org.springframework.web.reactive.BindingContext;
import org.springframework.web.server.PayloadTooLargeException; import org.springframework.web.server.ContentTooLargeException;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebInputException; import org.springframework.web.server.ServerWebInputException;
import org.springframework.web.server.UnsupportedMediaTypeStatusException; import org.springframework.web.server.UnsupportedMediaTypeStatusException;
@ -128,7 +128,7 @@ class MessageReaderArgumentResolverTests {
Mono<TestBean> result = (Mono<TestBean>) this.resolver.readBody( Mono<TestBean> result = (Mono<TestBean>) this.resolver.readBody(
param, true, this.bindingContext, exchange).block(); param, true, this.bindingContext, exchange).block();
StepVerifier.create(result).expectError(PayloadTooLargeException.class).verify(); StepVerifier.create(result).expectError(ContentTooLargeException.class).verify();
} }
@Test @Test

1
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityExceptionHandlerTests.java

@ -135,6 +135,7 @@ class ResponseEntityExceptionHandlerTests {
} }
@Test @Test
@SuppressWarnings("deprecation")
void handleResponseStatusException() { void handleResponseStatusException() {
testException(new ResponseStatusException(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED)); testException(new ResponseStatusException(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED));
} }

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

@ -211,11 +211,22 @@ public interface ServerResponse {
return status(HttpStatus.NOT_FOUND); return status(HttpStatus.NOT_FOUND);
} }
/**
* Create a builder with a
* {@linkplain HttpStatus#UNPROCESSABLE_CONTENT 422 Unprocessable Content} status.
* @return the created builder
*/
static BodyBuilder unprocessableContent() {
return status(HttpStatus.UNPROCESSABLE_CONTENT);
}
/** /**
* Create a builder with a * Create a builder with a
* {@linkplain HttpStatus#UNPROCESSABLE_ENTITY 422 Unprocessable Entity} status. * {@linkplain HttpStatus#UNPROCESSABLE_ENTITY 422 Unprocessable Entity} status.
* @return the created builder * @return the created builder
* @deprecated since 7.0 in favor of {@link #unprocessableContent()}
*/ */
@Deprecated(since = "7.0")
static BodyBuilder unprocessableEntity() { static BodyBuilder unprocessableEntity() {
return status(HttpStatus.UNPROCESSABLE_ENTITY); return status(HttpStatus.UNPROCESSABLE_ENTITY);
} }

7
spring-webmvc/src/main/kotlin/org/springframework/web/servlet/function/RouterFunctionDsl.kt

@ -812,9 +812,16 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD
*/ */
fun notFound() = ServerResponse.notFound() fun notFound() = ServerResponse.notFound()
/**
* @see ServerResponse.unprocessableContent
*/
fun unprocessableContent() = ServerResponse.unprocessableContent()
/** /**
* @see ServerResponse.unprocessableEntity * @see ServerResponse.unprocessableEntity
*/ */
@Deprecated("Use Unprocessable Content instead", ReplaceWith("unprocessableContent()"))
@Suppress("DEPRECATION")
fun unprocessableEntity() = ServerResponse.unprocessableEntity() fun unprocessableEntity() = ServerResponse.unprocessableEntity()
/** /**

Loading…
Cancel
Save