@ -64,16 +64,6 @@ public class MethodArgumentNotValidException extends BindException implements Er
}
}
@Override
public HttpStatusCode getStatusCode ( ) {
return HttpStatus . BAD_REQUEST ;
}
@Override
public ProblemDetail getBody ( ) {
return this . body ;
}
/ * *
/ * *
* Return the method parameter that failed validation .
* Return the method parameter that failed validation .
* /
* /
@ -82,59 +72,40 @@ public class MethodArgumentNotValidException extends BindException implements Er
}
}
@Override
@Override
public String getMessage ( ) {
public HttpStatusCode getStatusCode ( ) {
StringBuilder sb = new StringBuilder ( "Validation failed for argument [" )
return HttpStatus . BAD_REQUEST ;
. append ( this . parameter . getParameterIndex ( ) ) . append ( "] in " )
}
. append ( this . parameter . getExecutable ( ) . toGenericString ( ) ) ;
BindingResult bindingResult = getBindingResult ( ) ;
@Override
if ( bindingResult . getErrorCount ( ) > 1 ) {
public ProblemDetail getBody ( ) {
sb . append ( " with " ) . append ( bindingResult . getErrorCount ( ) ) . append ( " errors" ) ;
return this . body ;
}
sb . append ( ": " ) ;
for ( ObjectError error : bindingResult . getAllErrors ( ) ) {
sb . append ( '[' ) . append ( error ) . append ( "] " ) ;
}
return sb . toString ( ) ;
}
}
@Override
@Override
public Object [ ] getDetailMessageArguments ( ) {
public Object [ ] getDetailMessageArguments ( ) {
return new Object [ ] {
return new Object [ ] {
join ( formatErrors ( getGlobalErrors ( ) , null , null ) ) ,
join ( errorsToStringList ( getGlobalErrors ( ) ) ) ,
join ( formatErrors ( getFieldErrors ( ) , null , null ) ) } ;
join ( errorsToStringList ( getFieldErrors ( ) ) ) } ;
}
}
@Override
@Override
public Object [ ] getDetailMessageArguments ( MessageSource messageSource , Locale locale ) {
public Object [ ] getDetailMessageArguments ( MessageSource messageSource , Locale locale ) {
return new Object [ ] {
return new Object [ ] {
join ( formatErrors ( getGlobalErrors ( ) , messageSource , locale ) ) ,
join ( errorsToStringList ( getGlobalErrors ( ) , messageSource , locale ) ) ,
join ( formatErrors ( getFieldErrors ( ) , messageSource , locale ) ) } ;
join ( errorsToStringList ( getFieldErrors ( ) , messageSource , locale ) ) } ;
}
}
private static String join ( List < String > errors ) {
private static String join ( List < String > errors ) {
return String . join ( ", and " , errors ) ;
return String . join ( ", and " , errors ) ;
}
}
/ * *
* Resolve global and field errors to messages with the given
* { @link MessageSource } and { @link Locale } .
* @return a Map with errors as keys and resolved messages as values
* @since 6 . 0 . 3
* /
public Map < ObjectError , String > resolveErrorMessages ( MessageSource source , Locale locale ) {
Map < ObjectError , String > map = new LinkedHashMap < > ( getErrorCount ( ) ) ;
getGlobalErrors ( ) . forEach ( error - > map . put ( error , formatError ( error , source , locale ) ) ) ;
getFieldErrors ( ) . forEach ( error - > map . put ( error , formatError ( error , source , locale ) ) ) ;
return map ;
}
/ * *
/ * *
* Convert each given { @link ObjectError } to a String in single quotes , taking
* Convert each given { @link ObjectError } to a String in single quotes , taking
* either the error ' s default message , or its error code .
* either the error ' s default message , or its error code .
* @since 6 . 0
* @since 6 . 0
* /
* /
public static List < String > errorsToStringList ( List < ? extends ObjectError > errors ) {
public static List < String > errorsToStringList ( List < ? extends ObjectError > errors ) {
return formatErrors ( errors , null , null ) ;
return errorsToStringList ( errors , null , null ) ;
}
}
/ * *
/ * *
@ -144,12 +115,6 @@ public class MethodArgumentNotValidException extends BindException implements Er
* @since 6 . 0
* @since 6 . 0
* /
* /
public static List < String > errorsToStringList (
public static List < String > errorsToStringList (
List < ? extends ObjectError > errors , @Nullable MessageSource source , Locale locale ) {
return formatErrors ( errors , source , locale ) ;
}
public static List < String > formatErrors (
List < ? extends ObjectError > errors , @Nullable MessageSource messageSource , @Nullable Locale locale ) {
List < ? extends ObjectError > errors , @Nullable MessageSource messageSource , @Nullable Locale locale ) {
return errors . stream ( )
return errors . stream ( )
@ -170,4 +135,33 @@ public class MethodArgumentNotValidException extends BindException implements Er
return ( field + message ) ;
return ( field + message ) ;
}
}
/ * *
* Resolve global and field errors to messages with the given
* { @link MessageSource } and { @link Locale } .
* @return a Map with errors as keys and resolved messages as values
* @since 6 . 0 . 3
* /
public Map < ObjectError , String > resolveErrorMessages ( MessageSource source , Locale locale ) {
Map < ObjectError , String > map = new LinkedHashMap < > ( getErrorCount ( ) ) ;
getGlobalErrors ( ) . forEach ( error - > map . put ( error , formatError ( error , source , locale ) ) ) ;
getFieldErrors ( ) . forEach ( error - > map . put ( error , formatError ( error , source , locale ) ) ) ;
return map ;
}
@Override
public String getMessage ( ) {
StringBuilder sb = new StringBuilder ( "Validation failed for argument [" )
. append ( this . parameter . getParameterIndex ( ) ) . append ( "] in " )
. append ( this . parameter . getExecutable ( ) . toGenericString ( ) ) ;
BindingResult bindingResult = getBindingResult ( ) ;
if ( bindingResult . getErrorCount ( ) > 1 ) {
sb . append ( " with " ) . append ( bindingResult . getErrorCount ( ) ) . append ( " errors" ) ;
}
sb . append ( ": " ) ;
for ( ObjectError error : bindingResult . getAllErrors ( ) ) {
sb . append ( '[' ) . append ( error ) . append ( "] " ) ;
}
return sb . toString ( ) ;
}
}
}