Improve error handling in REST Controllers [SPR-14662] #19226
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
status: declined
A suggestion or change that we don't feel we should currently apply
type: enhancement
A general enhancement
Uh oh!
There was an error while loading. Please reload this page.
Sergey Derugo opened SPR-14662 and commented
It would be nice if there is default exception handler for REST controllers.
Motivation
Nowadays JSON response is extremely popular for REST services. It's very nice that
@RestController
and@ResponseBody
annotations provide an easy way to generate JSON response. But there is an issue if any exception occurs in such controllers - the server response contains HTML with the stack trace. This is not convenient for the consumers of such REST service because they expect to get JSON.The workaround is either define error handler for each method(and this handler will convert the error to appropriate JSON) - but this is not convenient do it for each method. Another solution is implement such global exception handler using
@ControllerAdvice
Proposed solution
Default exception handler should exist in the Spring MVC for REST controllers. If there is an exception in the REST controller then it should return HTTP response with error JSON, for example:
{
success: false,
statusCode: 500,
errorCode: "<guid>" /* auto-generated by Error handler to facilitate search in the log*/,
errorDetails: "Stack-trace etc." /*Exception stack trace. Might be turned off for production deployment */
}
Issue Links:
The text was updated successfully, but these errors were encountered: