Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

Exception raised by interceptor preHandle(),postHandle() and handler.

HandlerExceptionResolver

At the time of initialization of DispatcherServlet, automatically a Collection of


HandlerExceptionResolver is automatically created.Note there is no by default
HandlerExceptionResolver.we need to configure our Exception inside the
HandlerExceptionResolver.

If any exception is raise then spring framework check, for the corresponding
exception is there any HandlerExceptionResolver in ApplicationContext or not.If not
then it simply returns the same exception back to the client.

HandlerExceptionResolver having a method called as resolveException() which mostly


contain the logic for handling the exception.
So spring framework automatically invoke resolveException().If resolveException
returns null then spring framework switch to next HandlerExceptionResolver from the
Collection.If it returns ModelAndView then it render a response by using
ModelAndView and return back to the Client.

There are so many implementation of HandlerExceptionResolver like


1. ExceptionHandlerExceptionResolver
2. DefaultHandlerExceptionResolver
3. ResponseStatusExceptionResolver
4. SimpleMappingExceptionResolver
5. AnnotationMethodHandlerExceptionResolver
6. Custom HandlerExceptionResolver

Before Spring 3.2: HandlerExceptionResolver or the @ExceptionHandler annotation.


After Spring 3.2: @ControllerAdvice

1.The Controller level @ExceptionHandler


========================================
JaxRs: javax.ws.rs.WebApplicationException
DrawBack:This is not a Global ExceptionHandler.It's applicable to only for a
particular controller.

public class FooController{


...
@ExceptonHandler({ CustomExcepton1.class, CustomExcepton2.class })
public void handleExcepton() {
//
}
}

2.The HandlerExceptionResolver
==============================
JaxRs:ExceptionMapper
Exception handle at Application level.

1. ExceptionHandlerExceptionResolver
=======================================
Spring 3.1 similar to @ExceptionHandler

2. DefaultHandlerExceptionResolver
==================================
Spring 3.0: Map Spring Exception to its corresponding HTTP Status code

Client error – 4xx and Server error – 5xx status codes.

It doesn't write any information on Response object.So here programmer take the
responsiblility to create a ModelAndView object and render information on the View.

3. ResponseStatusExceptionResolver
==================================
The exception classes which we create for our application, can be annotated with
@ResponseStatus. In that case an unhandled exception returned to the client will
have the specified HTTP status code.

4. SimpleMappingExceptionResolver
=================================
Maps Exception class names to view names. Old way to handle Exception.

5. AnnotationMethodHandlerExceptionResolver
============================================
Handle Exception through @ExceptionHandler.

6. Custom HandlerExceptionResolver
=======================================

public class ApiError{


private HttpStatus status;
private String message;
private List<String> errors;

public ApiError(HttpStatus status,String message,List<String>errors){


super();
this.status=status;
this.message=message;
this.errors=errors;
}

public ApiError(HttpStatus status,String message,String error){


super();
this.status=status;
this.message=message;
errors=Arrays.asList(error);
}
}

You might also like