Spring Boot @ExceptionHandler

Spring Boot @ExceptionHandler tutorial shows how to handle exceptions with Spring @ExceptionHandler.

Spring is a popular Java application framework and Spring Boot is an evolution of Spring that helps create stand-alone, production-grade Spring based applications easily.

@ExceptionHandler is an annotation for handling exceptions in specific handler classes or handler methods. In Servlet environments, we can combine the @ExceptionHandler annotation with @ResponseStatus to define the response status for the HTTP response.

Spring Boot @ExceptionHandler example

In the following application, we demonstrate the usage of the @ExceptionHandler. A HTML link in the home page calls a controller's method, which either returns data or throws an exception.

$ tree
.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           ├── Application.java
    │   │           ├── controller
    │   │           │   └── MyController.java
    │   │           ├── exception
    │   │           │   └── MyDataException.java
    │   │           └── service
    │   │               ├── IDataService.java
    │   │               └── MyDataService.java
    │   └── resources
    │       ├── static
    │       │   ├── index.html
    │       │   └── showError.html
    │       └── templates
    │           └── showData.ftl
    └── test
        └── java

This is the project structure of the Spring application.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zetcode</groupId>
    <artifactId>SpringBootExceptionHandler</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>    
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>        
</project>

This is the Maven pom.xml file. The spring-boot-starter-freemarker is a dependency for Freemarker template engine; the spring-boot-maven-plugin packages Spring applications into executable JAR or WAR archives.

MyController.java
package com.zetcode.controller;

import com.zetcode.exception.MyDataException;
import com.zetcode.service.MyDataService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {
    
    @Autowired
    private MyDataService dataService;

    @RequestMapping(value = "/getData")
    public ModelAndView getData() {
        
        List<String> data = dataService.findAll();
        
        Map<String, Object> params = new HashMap<>();
        params.put("values", data);        
        
        return new ModelAndView("showData", params);
    }
    
    @ExceptionHandler(MyDataException.class)
    public String handleError(MyDataException e) {
        
        return "redirect:/showError.html";
    }    
}

The MyController's getData() method calls a service method and stores the retrieved data into a list. The data is sent to the showData view. In case of a MyDataException, the controller redirects to an error page.

@ExceptionHandler(MyDataException.class)
public String handleError(MyDataException e) {
    
    return "redirect:/showError.html";
}    

The handleError() is decorated with @ExceptionHandler. The handler is activated for the MyDataException. In the body of the method, we redirect to the showError.html page.

MyDataException.java
package com.zetcode.exception;

public class MyDataException extends RuntimeException {

    public MyDataException(String message) {
        super(message);
    }
}

We define a custom MyDataException.

IDataService.java
package com.zetcode.service;

import java.util.List;

public interface IDataService {
    
    public List<String> findAll(); 
}

IDataService contains the contract method.

MyDataService.java
package com.zetcode.service;

import com.zetcode.exception.MyDataException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.springframework.stereotype.Service;

@Service
public class MyDataService implements IDataService {

    @Override
    public List<String> findAll() {
        
        Random r = new Random();
        
        if (r.nextBoolean()) {
            
            throw new MyDataException("Failed to retrieve data");
        }

        List<String> data = new ArrayList<>();
        data.add("yellow moon");
        data.add("brisk pace");
        data.add("empty bottle");
        data.add("beautiful weather");

        return data;
    }
}

MyDataService implements the IDataService's findAll() method. The method either returns data or throws a MyDataException.

Random r = new Random();

if (r.nextBoolean()) {
    
    throw new MyDataException("Failed to retrieve data");
}

The findAll() method randomly throws MyDataException. The exception is then handled in the controller.

List<String> data = new ArrayList<>();
data.add("yellow moon");
data.add("brisk pace");
data.add("empty bottle");
data.add("beautiful weather");

return data;

When there is no exception, we return a list of strings.

Application.java
package com.zetcode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application  {
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Application is the entry point which sets up Spring Boot application.

index.html
<!DOCTYPE html>
<html>
    <head>
        <title>Home page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <a href="/getData">Get data</a>
    </body>
</html>

This is the home page. It contains a link that calls our controller method to fetch some data.

showError.html
<!DOCTYPE html>
<html>
    <head>
        <title>Error</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <p>Failed to retrieve data</p>
    </body>
</html>

This is an error page. It is shown when MyDataException is thrown.

showData.ftl
<!DOCTYPE html>
<html>
    <head>
        <title>Data</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    
    <body>

        <h2>Data</h2>
            <ul>
                <#list values as val>
                    <li>${val}</td> 
                </#list>        
            </ul>        
        </table>                
    </body>
</html>

The showData.ftl is a Freemarker template file which shows all retrieved data in an HTML list.

In this tutorial, we have shown how to handle exceptions in a Spring application with @ExceptionHandler. You might also be interested in the related tutorials: Spring Boot @ResponseStatus tutorial, Spring Boot upload file, Spring Boot @PathVariable tutorial, Spring Boot @RequestParam tutorial, Spring Boot REST H2 tutorial, Standalone Spring applications, Java tutorial.