33import java .util .ArrayList ;
44import java .util .List ;
55
6+ import javax .validation .ConstraintViolation ;
7+ import javax .validation .ConstraintViolationException ;
8+
69import org .springframework .beans .TypeMismatchException ;
710import org .springframework .http .HttpHeaders ;
811import org .springframework .http .HttpStatus ;
@@ -67,34 +70,50 @@ protected ResponseEntity<Object> handleTypeMismatch(final TypeMismatchException
6770 return new ResponseEntity <Object >(apiError , new HttpHeaders (), apiError .getStatus ());
6871 }
6972
70- @ ExceptionHandler ({ MethodArgumentTypeMismatchException . class })
71- public ResponseEntity <Object > handleMethodArgumentTypeMismatch (final MethodArgumentTypeMismatchException ex , final WebRequest request ) {
73+ @ Override
74+ protected ResponseEntity <Object > handleMissingServletRequestPart (final MissingServletRequestPartException ex , final HttpHeaders headers , final HttpStatus status , final WebRequest request ) {
7275 logger .info (ex .getClass ().getName ());
7376 //
74- final String error = ex .getName () + " should be of type " + ex .getRequiredType ().getName ();
75-
77+ final String error = ex .getRequestPartName () + " part is missing" ;
7678 final ApiError apiError = new ApiError (HttpStatus .BAD_REQUEST , ex .getLocalizedMessage (), error );
7779 return new ResponseEntity <Object >(apiError , new HttpHeaders (), apiError .getStatus ());
7880 }
7981
8082 @ Override
81- protected ResponseEntity <Object > handleMissingServletRequestPart (final MissingServletRequestPartException ex , final HttpHeaders headers , final HttpStatus status , final WebRequest request ) {
83+ protected ResponseEntity <Object > handleMissingServletRequestParameter (final MissingServletRequestParameterException ex , final HttpHeaders headers , final HttpStatus status , final WebRequest request ) {
8284 logger .info (ex .getClass ().getName ());
8385 //
84- final String error = ex .getRequestPartName () + " part is missing" ;
86+ final String error = ex .getParameterName () + " parameter is missing" ;
8587 final ApiError apiError = new ApiError (HttpStatus .BAD_REQUEST , ex .getLocalizedMessage (), error );
8688 return new ResponseEntity <Object >(apiError , new HttpHeaders (), apiError .getStatus ());
8789 }
8890
89- @ Override
90- protected ResponseEntity <Object > handleMissingServletRequestParameter (final MissingServletRequestParameterException ex , final HttpHeaders headers , final HttpStatus status , final WebRequest request ) {
91+ //
92+
93+ @ ExceptionHandler ({ MethodArgumentTypeMismatchException .class })
94+ public ResponseEntity <Object > handleMethodArgumentTypeMismatch (final MethodArgumentTypeMismatchException ex , final WebRequest request ) {
9195 logger .info (ex .getClass ().getName ());
9296 //
93- final String error = ex .getParameterName () + " parameter is missing" ;
97+ final String error = ex .getName () + " should be of type " + ex .getRequiredType ().getName ();
98+
9499 final ApiError apiError = new ApiError (HttpStatus .BAD_REQUEST , ex .getLocalizedMessage (), error );
95100 return new ResponseEntity <Object >(apiError , new HttpHeaders (), apiError .getStatus ());
96101 }
97102
103+
104+ @ ExceptionHandler ({ ConstraintViolationException .class })
105+ public ResponseEntity <Object > handleConstraintViolation (final ConstraintViolationException ex , final WebRequest request ) {
106+ logger .info (ex .getClass ().getName ());
107+ //
108+ final List <String > errors = new ArrayList <String >();
109+ for (final ConstraintViolation <?> violation : ex .getConstraintViolations ()) {
110+ errors .add (violation .getRootBeanClass ().getName () + " " + violation .getPropertyPath () + ": " + violation .getMessage ());
111+ }
112+
113+ final ApiError apiError = new ApiError (HttpStatus .BAD_REQUEST , ex .getLocalizedMessage (), errors );
114+ return new ResponseEntity <Object >(apiError , new HttpHeaders (), apiError .getStatus ());
115+ }
116+
98117 // 404
99118
100119 @ Override
@@ -116,7 +135,7 @@ protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(final HttpR
116135 final StringBuilder builder = new StringBuilder ();
117136 builder .append (ex .getMethod ());
118137 builder .append (" method is not supported for this request. Supported methods are " );
119- ex .getSupportedMethods ( );
138+ ex .getSupportedHttpMethods (). forEach ( t -> builder . append ( t + " " ) );
120139
121140 final ApiError apiError = new ApiError (HttpStatus .METHOD_NOT_ALLOWED , ex .getLocalizedMessage (), builder .toString ());
122141 return new ResponseEntity <Object >(apiError , new HttpHeaders (), apiError .getStatus ());
@@ -131,7 +150,7 @@ protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(final HttpMedia
131150 final StringBuilder builder = new StringBuilder ();
132151 builder .append (ex .getContentType ());
133152 builder .append (" media type is not supported. Supported media types are " );
134- ex .getSupportedMediaTypes ().forEach (t -> builder .append (t + ", " ));
153+ ex .getSupportedMediaTypes ().forEach (t -> builder .append (t + " " ));
135154
136155 final ApiError apiError = new ApiError (HttpStatus .UNSUPPORTED_MEDIA_TYPE , ex .getLocalizedMessage (), builder .substring (0 , builder .length () - 2 ));
137156 return new ResponseEntity <Object >(apiError , new HttpHeaders (), apiError .getStatus ());
@@ -143,6 +162,7 @@ protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(final HttpMedia
143162 @ ExceptionHandler ({ Exception .class })
144163 public ResponseEntity <Object > handleAll (final Exception ex , final WebRequest request ) {
145164 logger .info (ex .getClass ().getName ());
165+ logger .error ("error" , ex );
146166 //
147167 final ApiError apiError = new ApiError (HttpStatus .INTERNAL_SERVER_ERROR , ex .getLocalizedMessage (), "error occurred" );
148168 return new ResponseEntity <Object >(apiError , new HttpHeaders (), apiError .getStatus ());
0 commit comments