Class ExceptionHandlingAdvice

java.lang.Object
pro.nikolaev.restutils.components.ExceptionHandlingAdvice
Direct Known Subclasses:
PerControllerExceptionHandlingAdvice

@RestControllerAdvice public class ExceptionHandlingAdvice extends Object
Class representing RestControllerAdvice bean for handling MVC exception.
Version:
1.0
Author:
Ilya Nikolaev
  • Constructor Summary

    Constructors
    Constructor
    Description
    ExceptionHandlingAdvice(jakarta.servlet.MultipartConfigElement multipartConfigElement)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    org.springframework.http.ResponseEntity<ApiError>
    handle400(org.springframework.http.converter.HttpMessageNotReadableException e)
    ExceptionHandler to handle HttpMessageNotReadableException.
    org.springframework.http.ResponseEntity<ApiError>
    handle400(org.springframework.web.bind.MethodArgumentNotValidException e)
    ExceptionHandler to handle MethodArgumentNotValidException.
    org.springframework.http.ResponseEntity<ApiError>
    handle400(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException e)
    ExceptionHandler to handle MethodArgumentTypeMismatchException.
    org.springframework.http.ResponseEntity<ApiError>
    handle403(org.springframework.security.access.AccessDeniedException e)
    ExceptionHandler to handle AccessDeniedException.
    org.springframework.http.ResponseEntity<ApiError>
    handle404(org.springframework.web.servlet.resource.NoResourceFoundException e)
    ExceptionHandler to handle NoResourceFoundException.
    org.springframework.http.ResponseEntity<ApiError>
    handle405(org.springframework.web.HttpRequestMethodNotSupportedException ignored)
    ExceptionHandler to handle HttpRequestMethodNotSupportedException.
    org.springframework.http.ResponseEntity<ApiError>
    handle406(org.springframework.web.HttpMediaTypeNotAcceptableException e)
    ExceptionHandler to handle HttpMediaTypeNotAcceptableException.
    org.springframework.http.ResponseEntity<ApiError>
    handle413(org.springframework.web.multipart.MaxUploadSizeExceededException e)
    ExceptionHandler to handle MaxUploadSizeExceededException.
    org.springframework.http.ResponseEntity<ApiError>
    handle415(org.springframework.web.HttpMediaTypeNotSupportedException e)
    ExceptionHandler to handle HttpMediaTypeNotSupportedException.
    org.springframework.http.ResponseEntity<ApiError>
    ExceptionHandler to handle ApiException.
    org.springframework.http.ResponseEntity<ApiError>
    handleStatusException(org.springframework.web.server.ResponseStatusException e)
    ExceptionHandler to handle ResponseStatusException.
    org.springframework.http.ResponseEntity<ApiError>
    ExceptionHandler to handle any exception not specified in other handlers of ExceptionHandlingAdvice thrown while executing a RestController method.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ExceptionHandlingAdvice

      public ExceptionHandlingAdvice(jakarta.servlet.MultipartConfigElement multipartConfigElement)
  • Method Details

    • handleApiException

      @ExceptionHandler(ApiException.class) public org.springframework.http.ResponseEntity<ApiError> handleApiException(ApiException e)
      ExceptionHandler to handle ApiException.
      Parameters:
      e - ApiException to be processed by ExceptionHandler
      Returns:
      ResponseEntity with HTTP status from exception, exception reason as message, exception message in details part of the body and Connection: Close header
      Since:
      1.0
      See Also:
    • handle405

      @ExceptionHandler(org.springframework.web.HttpRequestMethodNotSupportedException.class) public org.springframework.http.ResponseEntity<ApiError> handle405(org.springframework.web.HttpRequestMethodNotSupportedException ignored)
      ExceptionHandler to handle HttpRequestMethodNotSupportedException.
      Parameters:
      ignored - HttpRequestMethodNotSupportedException to be processed by ExceptionHandler
      Returns:
      ResponseEntity with HTTP status 405, "Метод не поддерживается" message and Connection: Close header
      Since:
      1.0
      See Also:
      • ExceptionHandler
      • HttpRequestMethodNotSupportedException
      • ResponseEntity
    • handle400

      @ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class) public org.springframework.http.ResponseEntity<ApiError> handle400(org.springframework.web.bind.MethodArgumentNotValidException e)
      ExceptionHandler to handle MethodArgumentNotValidException.

      NOTE: only works if Jakarta Bean Validation is properly configured.

      Parameters:
      e - MethodArgumentNotValidException to be processed by ExceptionHandler
      Returns:
      ResponseEntity with HTTP status 400, "Некорректный запрос" message, failed parameter info in details part of the body and Connection: Close header
      Since:
      1.0
      See Also:
      • ExceptionHandler
      • MethodArgumentNotValidException
      • ResponseEntity
    • handle400

      @ExceptionHandler(org.springframework.http.converter.HttpMessageNotReadableException.class) public org.springframework.http.ResponseEntity<ApiError> handle400(org.springframework.http.converter.HttpMessageNotReadableException e)
      ExceptionHandler to handle HttpMessageNotReadableException.
      Parameters:
      e - HttpMessageNotReadableException to be processed by ExceptionHandler
      Returns:
      ResponseEntity with HTTP status 400, "Некорректный запрос" message, exception message in details part of the body and Connection: Close header
      Since:
      1.0
      See Also:
      • ExceptionHandler
      • HttpMessageNotReadableException
      • ResponseEntity
    • handle400

      @ExceptionHandler(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException.class) public org.springframework.http.ResponseEntity<ApiError> handle400(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException e)
      ExceptionHandler to handle MethodArgumentTypeMismatchException.
      Parameters:
      e - MethodArgumentTypeMismatchException to be processed by ExceptionHandler
      Returns:
      ResponseEntity with HTTP status 400, "Некорректный запрос" message, information about invalid parameter in details part of the body and Connection: Close header
      Since:
      1.0
      See Also:
      • ExceptionHandler
      • MethodArgumentTypeMismatchException
      • ResponseEntity
    • handle406

      @ExceptionHandler(org.springframework.web.HttpMediaTypeNotAcceptableException.class) public org.springframework.http.ResponseEntity<ApiError> handle406(org.springframework.web.HttpMediaTypeNotAcceptableException e)
      ExceptionHandler to handle HttpMediaTypeNotAcceptableException.
      Parameters:
      e - HttpMediaTypeNotAcceptableException to be processed by ExceptionHandler
      Returns:
      ResponseEntity with HTTP status 406, "Тип данных не поддерживается" message, exception message in details part of the body and Connection: Close header
      Since:
      1.0
      See Also:
      • ExceptionHandler
      • HttpMediaTypeNotAcceptableException
      • ResponseEntity
    • handle415

      @ExceptionHandler(org.springframework.web.HttpMediaTypeNotSupportedException.class) public org.springframework.http.ResponseEntity<ApiError> handle415(org.springframework.web.HttpMediaTypeNotSupportedException e)
      ExceptionHandler to handle HttpMediaTypeNotSupportedException.
      Parameters:
      e - HttpMediaTypeNotSupportedException to be processed by ExceptionHandler
      Returns:
      ResponseEntity with HTTP status 415, "Не поддерживаемый тип данных" message, exception message in details part of the body and Connection: Close header
      Since:
      1.0
      See Also:
      • ExceptionHandler
      • HttpMediaTypeNotSupportedException
      • ResponseEntity
    • handle413

      @ExceptionHandler(org.springframework.web.multipart.MaxUploadSizeExceededException.class) public org.springframework.http.ResponseEntity<ApiError> handle413(org.springframework.web.multipart.MaxUploadSizeExceededException e)
      ExceptionHandler to handle MaxUploadSizeExceededException.
      Parameters:
      e - MaxUploadSizeExceededException to be processed by ExceptionHandler
      Returns:
      ResponseEntity with HTTP status 413, "Превышен максимальный размер запроса" message, either max request size, max file size or both in details part of the body and Connection: Close header
      Since:
      1.0
      See Also:
      • ExceptionHandler
      • MaxUploadSizeExceededException
      • ResponseEntity
    • handleStatusException

      @ExceptionHandler(org.springframework.web.server.ResponseStatusException.class) public org.springframework.http.ResponseEntity<ApiError> handleStatusException(org.springframework.web.server.ResponseStatusException e)
      ExceptionHandler to handle ResponseStatusException.
      Parameters:
      e - ResponseStatusException to be processed by ExceptionHandler
      Returns:
      ResponseEntity with HTTP status from exception, exception reason as message, exception detailed message code in details part of the body and Connection: Close header
      Since:
      1.0
      See Also:
      • ExceptionHandler
      • ResponseStatusException
      • ResponseEntity
    • handle404

      @ExceptionHandler(org.springframework.web.servlet.resource.NoResourceFoundException.class) public org.springframework.http.ResponseEntity<ApiError> handle404(org.springframework.web.servlet.resource.NoResourceFoundException e)
      ExceptionHandler to handle NoResourceFoundException.
      Parameters:
      e - NoResourceFoundException to be processed by ExceptionHandler
      Returns:
      ResponseEntity with HTTP status 404, "Не найдено" message, resource path in details part of the body and Connection: Close header
      Since:
      1.0
      See Also:
      • ExceptionHandler
      • NoResourceFoundException
      • ResponseEntity
    • handleUnexpectedException

      @ExceptionHandler(Exception.class) public org.springframework.http.ResponseEntity<ApiError> handleUnexpectedException(Exception e)
      ExceptionHandler to handle any exception not specified in other handlers of ExceptionHandlingAdvice thrown while executing a RestController method.

      As this method is intended to handle any unexpected or otherwise handled exceptions it also logs them at ERROR level for easier debugging.

      Parameters:
      e - Exception to be processed by ExceptionHandler
      Returns:
      ResponseEntity with HTTP status 500, "Внутренняя ошибка приложения" message, exception message in details part of the body and Connection: Close header
      Since:
      1.0
      See Also:
      • ExceptionHandler
      • ResponseEntity
    • handle403

      @ExceptionHandler(org.springframework.security.access.AccessDeniedException.class) public org.springframework.http.ResponseEntity<ApiError> handle403(org.springframework.security.access.AccessDeniedException e)
      ExceptionHandler to handle AccessDeniedException.
      Parameters:
      e - AccessDeniedException to be processed by ExceptionHandler
      Returns:
      ResponseEntity with HTTP status 403, "Доступ запрещен" message, resource path in details part of the body and Connection: Close header
      Since:
      1.0.5
      See Also:
      • ExceptionHandler
      • AccessDeniedException
      • ResponseEntity