Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Sep 2, 2016 · 1 comment
Closed
Assignees
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

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Sep 2, 2016

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:

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 5, 2017

Rossen Stoyanchev commented

hi, thanks for the suggestion but this has been considered before, #17136 probably has the most extensive description but there are a few related tickets.

The short version is that the response content is application-specific. We provide ResponseEntityExceptionHandler as a base class that you can extend and override one method to determine the response body. When such a class is annotated with @ControllerAdvice as expected (and described in the Javadoc) it would be global across all controllers. You can even use the annotation attributes to apply to @RestController classes only.

@spring-projects-issues spring-projects-issues added status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

2 participants