Spring @ExceptionHandler - apply to json producing @RequestMapping only -
i have exception handler produce json error object consumed javascript in view.
@controlleradvice public class exceptionhandlercontroller { @exceptionhandler(value = exception.class) @responsebody public final jsonresponse<void> handlejsonexception( final exception e, final httpservletrequest request, final httpservletresponse response) { response.setstatus(httpservletresponse.sc_internal_server_error); return new errorjsonresponse(e); } }
i don't want method run regular postback (non json) requests. have these sorts of exception handled web.xml configured error pages.
<error-page> <error-code>500</error-code> <location>/500</location> </error-page>
in @requestmapping methods return json explicitly set produces value.
@requestmapping(value = "/dosomething", method = requestmethod.post, produces = mediatype.application_json_value)
how can tell spring use particular exception handler based on produces content type?
@controlleradvice
annotated classes assist known controllers in application. developers restrict exceptionhandlers cases using specific business/technical exceptions; in case, exception.class
broad.
but use case valid - in fact, issue has been resolved (see spr-10222) upcoming spring 4.0 release.
you example have restexceptionhandlercontroller annotated @controlleradvice(annotations = restcontroller.class)
- given rest controllers annotated @restcontroller
(annotation implies @responsebody
on methods!).
however, new feature doesn't rely on mediatypes (negotiated @ runtime, depending on client's request) on annotations, assignabletypes, packages, base package classes... think structure @controlleradvice
annotated classes.
Comments
Post a Comment