diff --git a/spring-web/src/main/java/org/springframework/web/ErrorResponse.java b/spring-web/src/main/java/org/springframework/web/ErrorResponse.java index 11d8375a11..7b2d335b18 100644 --- a/spring-web/src/main/java/org/springframework/web/ErrorResponse.java +++ b/spring-web/src/main/java/org/springframework/web/ErrorResponse.java @@ -29,14 +29,14 @@ import org.springframework.http.ProblemDetail; *

{@link ErrorResponseException} is a default implementation of this * interface and a convenient base class for other exceptions to use. * - *

An {@code @ExceptionHandler} method can use - * {@link org.springframework.http.ResponseEntity#of(ErrorResponse)} to map an - * {@code ErrorResponse} to a {@code ResponseEntity}. + *

{@code ErrorResponse} is supported as a return value from + * {@code @ExceptionHandler} methods that render directly to the response, e.g. + * by being marked {@code @ResponseBody}, or declared in an + * {@code @RestController} or {@code RestControllerAdvice} class. * * @author Rossen Stoyanchev * @since 6.0 * @see ErrorResponseException - * @see org.springframework.http.ResponseEntity#of(ErrorResponse) */ public interface ErrorResponse { diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/MissingServletRequestPartException.java b/spring-web/src/main/java/org/springframework/web/multipart/support/MissingServletRequestPartException.java index 04595f8ad5..cd98d9176f 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/MissingServletRequestPartException.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/MissingServletRequestPartException.java @@ -16,7 +16,12 @@ package org.springframework.web.multipart.support; -import org.springframework.web.bind.ServletRequestBindingException; +import jakarta.servlet.ServletException; + +import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; +import org.springframework.http.ProblemDetail; +import org.springframework.web.ErrorResponse; import org.springframework.web.multipart.MultipartResolver; /** @@ -26,14 +31,22 @@ import org.springframework.web.multipart.MultipartResolver; * is not configured correctly for processing multipart requests, e.g. there * is no {@link MultipartResolver}. * + *

Note: This exception does not extend from + * {@link org.springframework.web.bind.ServletRequestBindingException} because + * it can also be raised at a lower level, i.e. from this package which does + * low level multipart request parsing, independent of higher level request + * binding features. + * * @author Rossen Stoyanchev * @since 3.1 */ @SuppressWarnings("serial") -public class MissingServletRequestPartException extends ServletRequestBindingException { +public class MissingServletRequestPartException extends ServletException implements ErrorResponse { private final String requestPartName; + private final ProblemDetail body = ProblemDetail.forStatus(getStatusCode()); + /** * Constructor for MissingServletRequestPartException. @@ -53,4 +66,22 @@ public class MissingServletRequestPartException extends ServletRequestBindingExc return this.requestPartName; } + /** + * Return the HTTP status code to use for the response. + */ + @Override + public HttpStatusCode getStatusCode() { + return HttpStatus.BAD_REQUEST; + } + + /** + * Return the body for the response, formatted as an RFC 7807 + * {@link ProblemDetail} whose {@link ProblemDetail#getStatus() status} + * should match the response status. + */ + @Override + public ProblemDetail getBody() { + return this.body; + } + }