Browse Source

Add MessageSource getters

See gh-29574
pull/29602/head
rstoyanchev 2 years ago
parent
commit
6c8fb6c204
  1. 13
      spring-web/src/main/java/org/springframework/web/ErrorResponse.java
  2. 9
      spring-web/src/main/java/org/springframework/web/bind/MethodArgumentNotValidException.java
  3. 5
      spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityExceptionHandler.java
  4. 5
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java

13
spring-web/src/main/java/org/springframework/web/ErrorResponse.java

@ -268,6 +268,19 @@ public interface ErrorResponse { @@ -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;
}
}
}

9
spring-web/src/main/java/org/springframework/web/bind/MethodArgumentNotValidException.java

@ -26,6 +26,7 @@ import org.springframework.core.MethodParameter; @@ -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 @@ -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
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityExceptionHandler.java

@ -75,6 +75,11 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa @@ -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
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java

@ -96,6 +96,11 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa @@ -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 .

Loading…
Cancel
Save