Created
July 16, 2021 14:08
-
-
Save daniel-shuy/212084f23bf67b357076374377dd7773 to your computer and use it in GitHub Desktop.
Get error messages from Spring BindingResult
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<String> getErrorMessages(BindingResult bindingResult) { | |
return bindingResult.getAllErrors() | |
.stream() | |
.map(error -> { | |
var defaultMessage = error.getDefaultMessage(); | |
if (error instanceof FieldError) { | |
var fieldError = (FieldError) error; | |
return String.format("%s %s", fieldError.getField(), defaultMessage); | |
} else { | |
return defaultMessage; | |
} | |
}) | |
.collect(Collectors.toList()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment