0

I'm getting this error message:

Error creating bean with name 'handlerExceptionResolver' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerExceptionResolver]: Factory method 'handlerExceptionResolver' threw exception; nested exception is java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class org.springframework.web.bind.MethodArgumentNotValidException]: {public org.springframework.http.ResponseEntity com.Internity.extraservice.CustomRestExceptionHandler.handleMethodArgumentNotValid(org.springframework.web.bind.MethodArgumentNotValidException,org.springframework.http.HttpHeaders,org.springframework.http.HttpStatus,org.springframework.web.context.request.WebRequest), public final org.springframework.http.ResponseEntity org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest) throws java.lang.Exception}

I'm using this code:

@ControllerAdvice
@RestControllerAdvice
public class CustomRestExceptionHandler extends ResponseEntityExceptionHandler{


   @ExceptionHandler(MethodArgumentNotValidException.class)
   protected ResponseEntity<Object> handleMethodArgumentNotValid(
     MethodArgumentNotValidException ex, 
     HttpHeaders headers, 
     HttpStatus status, 
     WebRequest request) {
       List<String> errors = new ArrayList<String>();
       for (FieldError error : ex.getBindingResult().getFieldErrors()) {
           errors.add(error.getField() + ": " + error.getDefaultMessage());
       }
       for (ObjectError error : ex.getBindingResult().getGlobalErrors()) {
           errors.add(error.getObjectName() + ": " + error.getDefaultMessage());
       }
    
       CustomApiError customApiError = 
         new CustomApiError(new Date(),HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors);
       return handleExceptionInternal(
         ex, customApiError, headers, customApiError.getStatus(), request);
    
   
 
   }


/*@ExceptionHandler({ Exception.class })
public ResponseEntity<Object> handleAll(Exception ex, WebRequest request) {
    CustomApiError customApiError = new CustomApiError(new Date(),HttpStatus.INTERNAL_SERVER_ERROR, ex.getLocalizedMessage(), "error occurred");
    return new ResponseEntity<Object>(
            customApiError, new HttpHeaders(), customApiError.getStatus());
}*/
}
Jugal
  • 68
  • 7

0 Answers0