This Spring Boot application demonstrates best practices for error handling in Java 21. It provides an HTTP endpoint that retrieves reports for users, showcasing various error handling techniques and exception management strategies.
- RESTful API for retrieving reports
- Comprehensive error handling with custom exceptions
- Simulated database and external service interactions
- OpenAPI documentation with SwaggerUI
The application follows a layered architecture:
- Controller Layer: Handles HTTP requests and responses
- Service Layer: Contains business logic and error handling
- Repository Layer: Simulates database access
- External Client: Simulates interaction with an external API
The application exposes the following endpoint:
GET /report?userId={userId}&reportName={reportName}
- Parameters:
userId: The ID of the user requesting the reportreportName: The name of the report to retrieve
- Response: A Report object containing the report data
- Error Responses:
- 404: User not found in database
- 404: Report name not found in report API
- 404: User not found in report API
- 500: Database access error
- 500: Unexpected error from report client
The application demonstrates several error handling techniques:
- Custom Exceptions: Domain-specific exceptions for different error scenarios
- Exception Translation: Converting low-level exceptions to more meaningful ones
- Proper HTTP Status Codes: Mapping exceptions to appropriate HTTP status codes
- Informative Error Messages: Clear error messages for troubleshooting
- Problem Details for HTTP APIs: Implementation of RFC 7807 standard for error responses
This application implements the Problem Details for HTTP APIs specification (RFC 7807) using Spring's ProblemDetail
class. This provides a standardized format for error responses, making them more consistent, machine-readable, and
developer-friendly.
Key aspects of our implementation:
- Standardized JSON Structure: All error responses follow the RFC 7807 format with properties like
type,title,status,detail, andinstance - Type URIs: Each error category has a unique URI identifier (e.g.,
https://api.error-handling.com/problems/user-not-found) - Consistent Error Handling: The
ErrorHandlerclass centralizes all exception handling and converts exceptions to appropriate Problem Detail responses - Descriptive Error Messages: Each error includes a human-readable description to aid troubleshooting
Benefits of using Problem Details:
- Improved API documentation and discoverability
- Better client error handling with machine-readable responses
- Consistent error format across the entire API
- Enhanced debugging capabilities
- Clone the repository
- Run
mvn spring-boot:run - Access the API at
http://localhost:8080/report?userId={userId}&reportName={reportName} - Access the SwaggerUI documentation at
http://localhost:8080/swagger-ui.html
The application flow:
- The controller receives a request with userId and reportName
- The UserService retrieves the user from the database (simulated)
- The ReportService uses the user's email to retrieve the report from an external API (simulated)
- Various exceptions may be thrown during this process, which are handled appropriately
- Java 21
- Spring Boot 3.4.5
- SpringDoc OpenAPI UI
- Spotless (code formatting)
- SpotBugs (code quality checking)
This project uses the following code quality tools:
Spotless is used for code formatting and ensuring consistent code style across the project. It's configured to use Google's Java Style Guide.
Key features:
- Formats Java code according to Google's Java Style Guide
- Removes unused imports
- Orders imports consistently
- Runs during the compile phase
To run Spotless manually:
mvn spotless:check # Check for formatting issues
mvn spotless:apply # Fix formatting issues
SpotBugs is used for static code analysis to find potential bugs and code quality issues.
Key features:
- Detects potential bugs and code quality issues
- Includes FindSecBugs plugin for security vulnerability detection
- Runs during the verify phase
- Fails the build if issues are found
To run SpotBugs manually:
mvn spotbugs:check # Run SpotBugs analysis
mvn spotbugs:gui # Open SpotBugs GUI to view issues