🚀 A Cleaner Way to Handle API Responses & Exceptions in Spring Boot (Need Feedback!) #9
PasinduOG
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi fellow developers! 👋
I've been working on a standardized API Response Wrapper and Global Exception Handler for Spring Boot applications to tackle some common pain points we all face.
😩 The Struggle
We often deal with:
💡 The Solution
I built a library that enforces a clean structure using the Builder Pattern for successes and RFC 7807 (ProblemDetail) for errors.
✨ Key Features
1. Fluent Builder Pattern
No more messy constructors or null passing! The API is fluent and readable.
2. Automatic Trace ID (MDC)
traceIdgeneration using SLF4J MDC.3. Global Exception Handling (RFC 7807)
MethodArgumentNotValidExceptionto customApiException.🛠️ Usage Example
Here is how clean the Controller looks:
The JSON Response:
{ "status": 200, "message": "User found", "content": { "id": 1, "name": "Pasindu" }, "timestamp": "2026-02-13T10:00:00Z", "traceId": "a1b2c3d4-e5f6-7890" }🤔 Request for Feedback
I'd love to hear your thoughts on this approach to improve it further!
ProblemDetailor a custom Error DTO?List<UserDto>)?Check out the code snippets or the repo here: https://github.com/PasinduOG/api-response
Thanks! 🚀
Beta Was this translation helpful? Give feedback.
All reactions