Description
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:
- Provide a standard exception hierarchy for REST end points [SPR-12531] #17136 Provide a standard exception hierarchy for REST end points