I have a requirement to use the NoHandlerFoundException exception handler within my global exception handler.
I was already using the MethodArgumentNotValidException handler by overriding it from the extended ResponseEntityExceptionHandler class.
Now, while running the application, I am getting this exception.
I was using the method Argument Not valid handler to handle the missing fields inside my request body.
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerExceptionResolver]: Factory method 'handlerExceptionResolver' threw exception with message: Ambiguous @ExceptionHandler method mapped for [class org.springframework.web.bind.MethodArgumentNotValidException]: {protected org.springframework.http.ResponseEntity com.kroger.iwt.coreservice.exception.handler.GlobalExceptionHandler.handleMethodArgumentNotValid(org.springframework.web.bind.MethodArgumentNotValidException,org.springframework.http.HttpHeaders,org.springframework.http.HttpStatusCode,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}
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler({ NoHandlerFoundException.class })
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<ErrorListResponse> notFound(final NoHandlerFoundException exception) {
log.error("{} {}", 404, exception.getMessage());
ErrorListResponse errorListResponse = errorResponse("Request URI Not Found", exception);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorListResponse);
}
// @Override
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
List<FieldError> errors = ex.getBindingResult().getFieldErrors();
List<ErrorListResponse.ErrorDetails> errorDetails = new ArrayList<>();
for (FieldError fieldError : errors) {
ErrorListResponse.ErrorDetails error = new ErrorListResponse.ErrorDetails();
error.setReason(fieldError.getDefaultMessage());
error.setCode("BAD_REQUEST");
error.setDatetime(new DateTimeResponse(Instant.now().toString(), "America/New_York"));
errorDetails.add(error);
}
ErrorListResponse errorResponse = new ErrorListResponse();
errorResponse.setErrors(errorDetails);
log.error("{} {}", 400, "Multiple fields are invalid");
return new ResponseEntity(errorResponse, HttpStatus.BAD_REQUEST);
}
}
As you are extending the
ResponseEntityExceptionHandlerclass and override its methods you should not use the@ExceptionHandlerannotation.Otherwise Spring's
org.springframework.web.servlet.HandlerExceptionResolverwill find more then one handler for the same exception class and report an error.This is valid also for methods that just handle those exceptions. Even with a different signature and/or in a different class not extending
ResponseEntityExceptionHandler. The exceptions are listed here:Solution
Remove
@ExceptionHandlerannotations from the methods handling the same exceptions as inResponseEntityExceptionHandler(listed above). For these exceptions you should always override the handler methods and not define your own ones.