From 6c8fb6c20490ef32deafce3f4389c45105827657 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Mon, 28 Nov 2022 10:59:29 +0000 Subject: [PATCH] Add MessageSource getters See gh-29574 --- .../java/org/springframework/web/ErrorResponse.java | 13 +++++++++++++ .../web/bind/MethodArgumentNotValidException.java | 9 +++++---- .../annotation/ResponseEntityExceptionHandler.java | 5 +++++ .../annotation/ResponseEntityExceptionHandler.java | 5 +++++ 4 files changed, 28 insertions(+), 4 deletions(-) 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 3c046a5a72c9..fc0fe4325b53 100644 --- a/spring-web/src/main/java/org/springframework/web/ErrorResponse.java +++ b/spring-web/src/main/java/org/springframework/web/ErrorResponse.java @@ -268,6 +268,19 @@ interface Builder { */ ErrorResponse build(); + /** + * Build the {@code ErrorResponse} instance and also resolve the "detail" + * and "title" through the given {@link MessageSource}. Effectively a + * shortcut for calling {@link #build()} and then + * {@link ErrorResponse#updateAndGetBody(MessageSource, Locale)}. + * @since 6.0.3 + */ + default ErrorResponse build(@Nullable MessageSource messageSource, Locale locale) { + ErrorResponse response = build(); + response.updateAndGetBody(messageSource, locale); + return response; + } + } } diff --git a/spring-web/src/main/java/org/springframework/web/bind/MethodArgumentNotValidException.java b/spring-web/src/main/java/org/springframework/web/bind/MethodArgumentNotValidException.java index 66c27ac32a91..65bcfaf68b57 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/MethodArgumentNotValidException.java +++ b/spring-web/src/main/java/org/springframework/web/bind/MethodArgumentNotValidException.java @@ -26,6 +26,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.http.ProblemDetail; +import org.springframework.lang.Nullable; import org.springframework.util.StringUtils; import org.springframework.validation.BindException; import org.springframework.validation.BindingResult; @@ -126,12 +127,12 @@ public static List errorsToStringList(List errors * back on the error's default message. * @since 6.0 */ - @SuppressWarnings("ConstantConditions") public static List errorsToStringList( - List errors, MessageSource source, Locale locale) { + List errors, @Nullable MessageSource source, Locale locale) { - return errorsToStringList(errors, error -> source.getMessage( - error.getCode(), error.getArguments(), error.getDefaultMessage(), locale)); + return (source != null ? + errorsToStringList(errors, error -> source.getMessage(error, locale)) : + errorsToStringList(errors)); } private static List errorsToStringList( diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityExceptionHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityExceptionHandler.java index 8bd42efbbf90..bdeb71240db4 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityExceptionHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityExceptionHandler.java @@ -75,6 +75,11 @@ public void setMessageSource(MessageSource messageSource) { this.messageSource = messageSource; } + @Nullable + public MessageSource getMessageSource() { + return this.messageSource; + } + /** * Handle all exceptions raised within Spring MVC handling of the request . diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java index 7e97cf5d0d98..b8b4208041e6 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java @@ -96,6 +96,11 @@ public void setMessageSource(MessageSource messageSource) { this.messageSource = messageSource; } + @Nullable + protected MessageSource getMessageSource() { + return this.messageSource; + } + /** * Handle all exceptions raised within Spring MVC handling of the request .