Announcement Announcement Module
No announcement yet.
Handling Exceptions As JSON [Spring 3.1] Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Handling Exceptions As JSON [Spring 3.1]

    My end goal is to have *Exception.class instances which are thrown by all of my controllers get turned into a specifically crafted Status object which is serialized as JSON using Jackson, with the HTTP status code intact.

    Currently, in spring 3.1, I can specify a method which will do that for me, but only scoped to the controller:
    public class MyController {
    	public Object doSomething() {
    		throw new RuntimeException("something bad happened");
    	public Status handleException(WebRequest req, Exception ex) {
    		return new Status(new Notification(Level.ERROR, ex.getMessage()));
    I looked at the HandlerExceptionResolver interface which returns a ModelAndView, and looked at the provided implementation in DefaultHandlerExceptionResolver and AnnotationMethodHandlerExceptionResolver and didn't see a clear way to plug my 'handleException' into a HandlerExceptionResolver without having to cut and paste most of the AnnotationMethodHandlerExceptionResolver that makes the @ResponseBody work.

    Alternatively, I could just make a base class for my controllers and include the annotated handleException method, but that's just bad design.

    Anyone know how to plug in an application scoped ExceptionHandler method that doesn't produce a ModelAndView?

    Last edited by amuraco; Dec 23rd, 2011, 10:58 AM.

  • #2
    Does anyone have ideas for alternative approaches for returning exceptions as Json?

    Is this a bad idea? I'm designing this for a Javascript front-end, what better approach could i use?

    I personally find the idea of wrapping all responses in a 'status' object and checking on the javascript client for a status.code == ERROR to be tedious compared to using a status object with http status code. Maybe I should do both? How can I cleanly set the Http Status code in a controller, doing response.setStatus(int) is ugly to have everywhere.. I guess I'm just looking for the wholesale approach for this with as little custom handling in the controllers as possible.


    • #3
      The support classes for annotated controllers in 3.1 have been superceded with newer versions that are more flexible and customizable (see the what's new in Spring 3.1 section). If you look at ExceptionHandlerExceptionResolver (the replacement for AnnotationMethodExceptionResolver) you will see that it delegates to RequestResponseBodyMethodProcessor to write an object to the response body. If you created your own HandlerExceptionResolver you could write your Status object that way.

      That should be sufficient but I'll also mention that with ExceptionHandlerExceptionResolver it is also possible to add the concept of a "global" @ExceptionHandler method (see the example here).


      • #4
        Thanks, I'll try this out!

        I know Spring 3.1 improved the exception handling, but I couldn't really figure out how it all goes together.