mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-05-03 04:19:47 +01:00
Add MessageSource getters
See gh-29574
This commit is contained in:
@@ -268,6 +268,19 @@ public interface ErrorResponse {
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-4
@@ -26,6 +26,7 @@ import org.springframework.core.MethodParameter;
|
||||
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 class MethodArgumentNotValidException extends BindException implements Er
|
||||
* back on the error's default message.
|
||||
* @since 6.0
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public static List<String> errorsToStringList(
|
||||
List<? extends ObjectError> errors, MessageSource source, Locale locale) {
|
||||
List<? extends ObjectError> 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<String> errorsToStringList(
|
||||
|
||||
+5
@@ -75,6 +75,11 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
|
||||
this.messageSource = messageSource;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public MessageSource getMessageSource() {
|
||||
return this.messageSource;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handle all exceptions raised within Spring MVC handling of the request .
|
||||
|
||||
+5
@@ -96,6 +96,11 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
|
||||
this.messageSource = messageSource;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected MessageSource getMessageSource() {
|
||||
return this.messageSource;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handle all exceptions raised within Spring MVC handling of the request .
|
||||
|
||||
Reference in New Issue
Block a user