Exception handling using @ControllerAdvice

Usually the evil stack trace appears to the user whenever any unknown exceptions occur to an application. The user will complain to our application as stack traces are not user friendly and not handled by the user at all. And sometimes these stack trace revile the internal method call which can cause security risks.

However, we can configure the web application deployment descriptor web.xml to display user-friendly JSP pages in case of class exception or HTTP errors. The Spring MVC provides a way to manage views in case of a class exception.

From Spring 3.2 onwards, we have the @ControllerAdvice annotation. This annotation is used to define the global exception handler using the @ExceptionHandler annotation. ...

Get Learning Spring Application Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.