javaintermediate
Spring Boot Global Exception Handler
Centralized error handling with @ControllerAdvice for validation errors, 404s, and custom exceptions.
javaPress ⌘/Ctrl + Shift + C to copy
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.http.*;
import java.time.Instant;
import java.util.*;
@RestControllerAdvice
public class GlobalExceptionHandler {
record ErrorResponse(int status, String error, String message, Instant timestamp) {}
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ErrorResponse> handleNotFound(ResourceNotFoundException ex) {
return ResponseEntity.status(404)
.body(new ErrorResponse(404, "Not Found", ex.getMessage(), Instant.now()));
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, Object>> handleValidation(MethodArgumentNotValidException ex) {
Map<String, String> fieldErrors = new HashMap<>();
ex.getBindingResult().getFieldErrors().forEach(err ->
fieldErrors.put(err.getField(), err.getDefaultMessage()));
Map<String, Object> body = Map.of(
"status", 400,
"error", "Validation Failed",
"fields", fieldErrors,
"timestamp", Instant.now()
);
return ResponseEntity.badRequest().body(body);
}
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ErrorResponse> handleBadRequest(IllegalArgumentException ex) {
return ResponseEntity.badRequest()
.body(new ErrorResponse(400, "Bad Request", ex.getMessage(), Instant.now()));
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleGeneric(Exception ex) {
return ResponseEntity.status(500)
.body(new ErrorResponse(500, "Internal Server Error",
"An unexpected error occurred", Instant.now()));
}
}
class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String resource, Object id) {
super(resource + " not found with id: " + id);
}
}Sponsored
Railway
Use Cases
- Consistent error responses across all endpoints
- Validation error formatting for frontend consumption
- Custom exception hierarchies in Spring APIs
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
javaintermediate
Spring Boot REST Controller with CRUD
Create a complete REST API with Spring Boot: GET, POST, PUT, DELETE with validation and error handling.
Best for: Building RESTful APIs with Spring Boot
#spring-boot#rest-api
javaintermediate
Java HttpClient — GET and POST Requests
Make HTTP requests with Java 11+ HttpClient: GET, POST JSON, async calls, and response handling.
Best for: Calling REST APIs from Java applications
#java#http
javaadvanced
Spring Security — JWT Authentication
Implement JWT authentication with Spring Security: token generation, validation, and filter chain.
Best for: Securing REST APIs with JWT tokens
#spring-boot#jwt
javaintermediate
Spring Boot — Custom Validator Annotation
Create custom validation annotations with ConstraintValidator for domain-specific field validation.
Best for: Domain-specific input validation
#spring-boot#validation