Add convenience methods for binding error messages

Closes gh-29574
This commit is contained in:
rstoyanchev
2022-12-06 13:36:49 +00:00
parent 5214bd3093
commit 918edaba2e
3 changed files with 85 additions and 17 deletions

View File

@@ -17,8 +17,10 @@
package org.springframework.web.bind;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import org.springframework.context.MessageSource;
@@ -97,20 +99,41 @@ public class MethodArgumentNotValidException extends BindException implements Er
@Override
public Object[] getDetailMessageArguments() {
return new Object[] {
errorsToStringList(getBindingResult().getGlobalErrors()),
errorsToStringList(getBindingResult().getFieldErrors())
};
return new Object[] {errorsToStringList(getGlobalErrors()), errorsToStringList(getFieldErrors())};
}
@Override
public Object[] getDetailMessageArguments(MessageSource messageSource, Locale locale) {
return new Object[] {
errorsToStringList(getBindingResult().getGlobalErrors(), messageSource, locale),
errorsToStringList(getBindingResult().getFieldErrors(), messageSource, locale)
errorsToStringList(getGlobalErrors(), messageSource, locale),
errorsToStringList(getFieldErrors(), messageSource, locale)
};
}
/**
* Resolve global and field errors to messages with the given
* {@link MessageSource} and {@link Locale}.
* @return a Map with errors as key and resolves messages as value
* @since 6.0.3
*/
public Map<ObjectError, String> resolveErrorMessages(MessageSource messageSource, Locale locale) {
Map<ObjectError, String> map = new LinkedHashMap<>();
addMessages(map, getGlobalErrors(), messageSource, locale);
addMessages(map, getFieldErrors(), messageSource, locale);
return map;
}
private static void addMessages(
Map<ObjectError, String> map, List<? extends ObjectError> errors,
MessageSource messageSource, Locale locale) {
List<String> messages = errorsToStringList(errors, messageSource, locale);
for (int i = 0; i < errors.size(); i++) {
map.put(errors.get(i), messages.get(i));
}
}
/**
* Convert each given {@link ObjectError} to a String in single quotes, taking
* either the error's default message, or its error code.

View File

@@ -17,6 +17,7 @@
package org.springframework.web.bind.support;
import java.beans.PropertyEditor;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -292,8 +293,8 @@ public class WebExchangeBindException extends ServerWebInputException implements
StringBuilder sb = new StringBuilder("Validation failed for argument at index ")
.append(parameter.getParameterIndex()).append(" in method: ")
.append(parameter.getExecutable().toGenericString())
.append(", with ").append(this.bindingResult.getErrorCount()).append(" error(s): ");
for (ObjectError error : this.bindingResult.getAllErrors()) {
.append(", with ").append(getErrorCount()).append(" error(s): ");
for (ObjectError error : getAllErrors()) {
sb.append('[').append(error).append("] ");
}
return sb.toString();
@@ -302,11 +303,35 @@ public class WebExchangeBindException extends ServerWebInputException implements
@Override
public Object[] getDetailMessageArguments(MessageSource source, Locale locale) {
return new Object[] {
MethodArgumentNotValidException.errorsToStringList(this.bindingResult.getGlobalErrors(), source, locale),
MethodArgumentNotValidException.errorsToStringList(this.bindingResult.getFieldErrors(), source, locale)
MethodArgumentNotValidException.errorsToStringList(getGlobalErrors(), source, locale),
MethodArgumentNotValidException.errorsToStringList(getFieldErrors(), source, locale)
};
}
/**
* Resolve global and field errors to messages with the given
* {@link MessageSource} and {@link Locale}.
* @return a Map with errors as key and resolves messages as value
* @since 6.0.3
*/
public Map<ObjectError, String> resolveErrorMessages(MessageSource messageSource, Locale locale) {
Map<ObjectError, String> map = new LinkedHashMap<>();
addMessages(map, getGlobalErrors(), messageSource, locale);
addMessages(map, getFieldErrors(), messageSource, locale);
return map;
}
private static void addMessages(
Map<ObjectError, String> map, List<? extends ObjectError> errors,
MessageSource messageSource, Locale locale) {
List<String> messages = MethodArgumentNotValidException.errorsToStringList(errors, messageSource, locale);
for (int i = 0; i < errors.size(); i++) {
map.put(errors.get(i), messages.get(i));
}
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || this.bindingResult.equals(other));