How to use Spring Exception Handler for SpringMVC: @ExceptionHandler, @ResponseStatus, @ControllerAdvice

Spring provides a cross-cutting concern solution for handling Java Excepion. So in the tutorial, JavaSampleApproach will guide how to use Spring Exception with: @ResponseStatus, @ExceptionHandler & @ControllerAdvice.
Related Articles:
How to handle Java Exception
How to use @RestControllerAdvice – New Features of Spring Framework 4.3

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE

II. Overview
1. @ResponseStatus

Normally, when backend server throws an unhandled Exception, a 500 Http response will be returned.
Spring provides a way to modify http code response by @ResponseStatus for customized Exception.
Sample:

2. @ExceptionHandler

We can use @ExceptionHandler with a method in any Controller to handle specific exceptions that throw from any @Requestmapping:
Sample:

– We can use @ResponseStatus with @ExceptionHandler to customize http response code.
– Use @ExceptionHandler to redirect to a html view page.
– Use @ExceptionHandler to build a completed ModelAndView.

3. @ControllerAdvice

We can use @ControllerAdvice to handle exception with globle scope of application, not only in an individial controller.
Sample:

Now handleCustomGeneralException method is active to handle any Exception with type CustomGeneralException.class in any Controller:

III. Practice
1. Create Spring Boot project

Create SpringBoot project with Thymeleaf view engine, add dependencies:

2. Create CustomExceptions

Create 4 customized Exception:
– 3 simple Customized Exception:

– 1 customized exception with @ResponseStatus:

3. Create simple views

Create 2 simple html views:
exception.html

generalexception.html

4. Create ControllerAdvice

5. Create Web Controller

6. Result

Run the project with Spring Boot App mode, then make requests:

http://localhost:8080/normalexception
Spring Exception - normal exception

http://localhost:8080/customexception
It will be handled by public String runtimeException() then shows exception view.
Spring Exception - customized exception

http://localhost:8080/conflictexception
It will be handle by public void conflict() method with @ResponseStatus so response status code is 409(HttpStatus.CONFLICT)
Spring Exception - conflict exception

http://localhost:8080/customexceptionwithhttptatuscode
The CustomExceptionWithHttpStatusCode modifies http code response @ResponseStatus(value=HttpStatus.NOT_FOUND, reason=”Not Found Exception!”) so result is: type=Not Found, status=404
Spring Exception - custom exception with http status code

http://localhost:8080/customgeneralexception
It will be handled by: public ModelAndView handleCustomGeneralException() method in GlobalExceptionHandler class. The result is a model with generalexception.html view.
Spring Exception - gerenal exception

IV. Sourcecode

SpringExceptionHandler


Related Posts



2 thoughts on “How to use Spring Exception Handler for SpringMVC: @ExceptionHandler, @ResponseStatus, @ControllerAdvice”

    1. Thank you very much!

      We are on the way to develop the site more helpful!
      You can share with your friends about our articles!

      Nice day!

Got Something To Say:

Your email address will not be published. Required fields are marked *

*