r/SpringBoot • u/rl_085 • Feb 07 '26
Question Error responses for REST API
Do you implement RFC 9457 in your error responses? I saw that Spring provides an abstraction for this with ProblemDetail, it looks good but not many people are using it.
12
Upvotes
6
u/smutje187 Feb 07 '26
I would suspect most API nowadays don’t particularly care about exposing detailed errors for their users for security or other reasons. Especially in machine to machine communication it’s questionable whether more detailed error messages are that useful in the first place as the caller would need to handle the different errors at all which over-complicates clients.
1
u/j0k3r_dev Feb 07 '26
That depends on the developer. Spring Web has had handlers for years; I find them convenient and I don't have to install anything. I just define RuntimeExceptions to keep the code clean, and that's it. You don't have to be just another sheep in the flock. Use what's most comfortable for you and does the job. There are many tools; just choose the one that best suits you. Personally, I use handlers. They're native, and I don't have to configure anything. When I want to return an error, I just throw a custom RuntimeException, catch it with the handler, and my code stays cleaner. That's perfect for me. You'll have to figure it out for yourself. One piece of advice: don't be a sheep; find what works best for you.
2
u/revilo-1988 Feb 07 '26
Actually, it's almost the gold standard in the industry right now.
1
u/barsay Senior Dev Feb 13 '26
We use Spring’s ProblemDetail as the base and shape responses to RFC 9457 (application/problem+json).
We keep the top-level fields stable (type/title/status/detail/instance) and add a small, safe extension: errorCode + extensions.errors[] (code/message/field/resource/id) for validation and domain errors — no stack traces / no internal exception details.
Reference implementation (handlers + client decoding): https://github.com/bsayli/spring-boot-openapi-generics-clients (see customer-service error handlers / RFC 9457 section). Hope it helps.
1
u/yaoyao127 Aug 08 '26
I like ProblemDetail. The biggest win for me is consistency.
Instead of every endpoint inventing its own error JSON, I’d handle it centrally with @ControllerAdvice and return the same RFC 9457-style structure everywhere.
I’m surprised it isn’t more common too. My only concern is making sure detail doesn’t accidentally expose internal exception messages.
1
u/wimdeblauwe Feb 07 '26
My library https://github.com/wimdeblauwe/error-handling-spring-boot-starter recently added support for this.
This article explains what the library does in more detail: https://foojay.io/today/better-error-handling-for-your-spring-boot-rest-apis/