Package io.github.pasinduog.exception
Class GlobalExceptionHandler
java.lang.Object
io.github.pasinduog.exception.GlobalExceptionHandler
@ConditionalOnProperty(prefix="api-response",
name="enabled",
havingValue="true",
matchIfMissing=true)
@RestControllerAdvice
public class GlobalExceptionHandler
extends Object
Global exception handler for Spring Boot REST APIs.
This class provides centralized exception handling using Spring's
RestControllerAdvice mechanism. It converts various exceptions
into RFC 7807 ProblemDetail responses with trace IDs for debugging.
Can be disabled by setting api-response.enabled=false in application properties.
- Since:
- 1.0.0
- Version:
- 2.0.0
- Author:
- Pasindu OG
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ProblemDetailHandles all unhandled exceptions.org.springframework.http.ProblemDetailHandles custom API exceptions.org.springframework.http.ProblemDetailhandleHttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException ex) Handles unsupported media type exceptions.org.springframework.http.ProblemDetailhandleHttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException ex) Handles malformed JSON request exceptions.org.springframework.http.ProblemDetailhandleHttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException ex) Handles HTTP method not supported exceptions.org.springframework.http.ProblemDetailhandleMethodArgumentTypeMismatchException(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException ex) Handles method argument type mismatch exceptions.org.springframework.http.ProblemDetailhandleMissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException ex) Handles missing required request parameter exceptions.org.springframework.http.ProblemDetailhandleNoResourceFoundException(org.springframework.web.servlet.resource.NoResourceFoundException ex) Handles 404 Not Found exceptions.org.springframework.http.ProblemDetailHandles null pointer exceptions.org.springframework.http.ProblemDetailhandleValidationExceptions(org.springframework.web.bind.MethodArgumentNotValidException ex) Handles validation exceptions from @Valid annotations.
-
Constructor Details
-
GlobalExceptionHandler
public GlobalExceptionHandler()Default constructor for Spring bean instantiation.
-
-
Method Details
-
handleAllExceptions
-
handleValidationExceptions
@ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class) public org.springframework.http.ProblemDetail handleValidationExceptions(org.springframework.web.bind.MethodArgumentNotValidException ex) Handles validation exceptions from @Valid annotations.- Parameters:
ex- the validation exception- Returns:
- ProblemDetail response with 400 status and field errors
-
handleMethodArgumentTypeMismatchException
@ExceptionHandler(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException.class) public org.springframework.http.ProblemDetail handleMethodArgumentTypeMismatchException(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException ex) Handles method argument type mismatch exceptions.- Parameters:
ex- the type mismatch exception- Returns:
- ProblemDetail response with 400 status
-
handleHttpMessageNotReadableException
@ExceptionHandler(org.springframework.http.converter.HttpMessageNotReadableException.class) public org.springframework.http.ProblemDetail handleHttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException ex) Handles malformed JSON request exceptions.- Parameters:
ex- the HTTP message not readable exception- Returns:
- ProblemDetail response with 400 status
-
handleMissingServletRequestParameterException
@ExceptionHandler(org.springframework.web.bind.MissingServletRequestParameterException.class) public org.springframework.http.ProblemDetail handleMissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException ex) Handles missing required request parameter exceptions.- Parameters:
ex- the missing parameter exception- Returns:
- ProblemDetail response with 400 status
-
handleNoResourceFoundException
@ExceptionHandler(org.springframework.web.servlet.resource.NoResourceFoundException.class) public org.springframework.http.ProblemDetail handleNoResourceFoundException(org.springframework.web.servlet.resource.NoResourceFoundException ex) Handles 404 Not Found exceptions.- Parameters:
ex- the no resource found exception- Returns:
- ProblemDetail response with 404 status
-
handleHttpRequestMethodNotSupportedException
@ExceptionHandler(org.springframework.web.HttpRequestMethodNotSupportedException.class) public org.springframework.http.ProblemDetail handleHttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException ex) Handles HTTP method not supported exceptions.- Parameters:
ex- the method not supported exception- Returns:
- ProblemDetail response with 405 status
-
handleHttpMediaTypeNotSupportedException
@ExceptionHandler(org.springframework.web.HttpMediaTypeNotSupportedException.class) public org.springframework.http.ProblemDetail handleHttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException ex) Handles unsupported media type exceptions.- Parameters:
ex- the media type not supported exception- Returns:
- ProblemDetail response with 415 status
-
handleNullPointerExceptions
@ExceptionHandler(NullPointerException.class) public org.springframework.http.ProblemDetail handleNullPointerExceptions(NullPointerException ex) Handles null pointer exceptions.- Parameters:
ex- the null pointer exception- Returns:
- ProblemDetail response with 500 status
-
handleApiException
@ExceptionHandler(ApiException.class) public org.springframework.http.ProblemDetail handleApiException(ApiException ex) Handles custom API exceptions.- Parameters:
ex- the API exception- Returns:
- ProblemDetail response with the exception's status code
-