Handling exceptions in Spring MVC by R4R Team

Handling exceptions in Spring MVC:-

Spring MVC provides several complimentary approaches to exception handling.Any web application requires good design for exception handling because we don’t want to serve container generated page when any unhandled exception is thrown by our application.

Exception handling approach is a huge point for any web application framework, that being said Spring MVC framework delivers well when it comes to exception and error handling in our web applications.
Spring MVC Framework provides following ways to help us achieving robust exception handling.

1 Controller Based Exception Handling:

Using @ExceptionHandler
We can define exception handler methods in our controller classes. All we need is to annotate these methods with @ExceptionHandler annotation. This annotation takes Exception class as argument. So if we have defined one of these for Exception class, then all the exceptions thrown by our request handler method will have handled.

You can add extra (@ExceptionHandler) methods to any controller to specifically handle exceptions thrown by request handling (@RequestMapping) methods in the same controller. Such methods can:

1 Handle exceptions without the @ResponseStatus annotation
2 Redirect the user to a dedicated error view
3 Build a totally custom error response

@Controller

public class SimpleController {

// @RequestMapping methods omitted ...

@ExceptionHandler(IOException.class)

public ResponseEntity<String> handleIOException(IOException ex) {

// prepare responseEntity

return responseEntity;

} }

2 Global Exception Handler:

Using @ControllerAdvice
A controller advice allows you to use exactly the same exception handling techniques but apply them across the whole application, not just to an individual controller. You can think of them as an annotation driven interceptor.

Any class annotated with @ControllerAdvice becomes a controller-advice and three types of method are supported:

1 Exception handling methods annotated with @ExceptionHandler.
2 Model enhancement methods (for adding additional data to the model) annotated with @ModelAttribute. Note that these attributes are not available to the exception handling views.
3 Binder initialization methods (used for configuring form-handling) annotated with @InitBinder.

3 HandlerExceptionResolver:

The generic exceptions, most of the times we serve static pages. Spring Framework provides HandlerExceptionResolver interface that we can implement to create global exception handler. The reason behind this additional way to define global exception handler is that Spring framework also provides default implementation classes that we can define in our spring bean configuration file to get spring framework exception handling benefits.
SimpleMappingExceptionResolver is the default implementation class, it allows us to configure exceptionMappings where we can specify which resource to use for a particular exception.

Any Spring bean declared in the DispatcherServlet's application context that implements HandlerExceptionResolver will be used to intercept and process any exception raised in the MVC system and not handled by a Controller. The interface looks like this:

public interface HandlerExceptionResolver {

ModelAndView resolveException(HttpServletRequest request,

HttpServletResponse response, Object handler, Exception ex);

}

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!