Skip to content

Add support for @Request-/@ResponseBody to @ExceptionHandler methods [SPR-7820] #12476

Closed

Description

@spring-projects-issues

Joris Kuipers opened SPR-7820 and commented

During a consultancy gig this came up: customer wants to be able to throw a custom exception from @Controller methods where the exception will contain an object that needs to be marshalled back to the client as JSON, since these methods respond to AJAX requests and thus the response will not be rendered directly by the browser.
I tried to do this by adding a method to an abstract parent class of the relevant controllers that looked like this:

@ExceptionHandler(MyCustomValidationErrorsException.class)
@ResponseBody
public MyCustomValidationErrors handleValidationErrors(MyCustomValidationErrorsException e) {
    return e.getValidationErrors();
}

This prevents regular methods from having to specify Object as the return type, since they would otherwise need to return either a regular domain object or a MyCustomValidationErrors instance in the case of validation errors.

However, at the moment @ResponseBody is not supported for @ExceptionHandler methods, so this will simply ignore the annotation and render a view using the default view name resolution algorithm. I think this would be a very useful addition for the sketched use case, so I was wondering if this support could be added to the framework.
I understand that there are other ways to achieve the desired functionality, but AFAICT it would require a relatively large amount of custom code that would duplicate much of the Spring MVC-provided functionality.


Affects: 3.0.5

Issue Links:

Activity

  1. spring-projects-issues commented on Jan 10, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Arjen, it seems this one can be closed now too since #11567 is now fixed.

  2. added
    type: enhancement
    status: duplicate
    in: web
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webstatus: duplicatetype: enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions