Skip to content

Improve error handling in REST Controllers [SPR-14662] #19226

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions