11. Exception handling in springboot project

Background analysis

In the development of the project, whether it is the operation process of the underlying data logic, the processing process of business logic or the processing process of control logic, it is inevitable to encounter all kinds of predictable and unpredictable exceptions. Handling exceptions well can protect the system and greatly improve the user experience.

Exception handling analysis

summary

There are no more than two ways to handle exceptions in Java projects, either to execute the trycatch operation or to execute the throw operation (throw it to other objects for processing). No matter which way is adopted, the purpose is to make our system have feedback on exceptions. But now the question is how to make the feedback code simple, intuitive and friendly.

Processing specification

In the process of handling exceptions, we usually follow certain design specifications, such as:

  • When catching an exception, it must exactly match the exception thrown, or the caught exception is the parent type of the exception thrown.
  • Avoid throwing RuntimeException directly, and it is not allowed to throw Exception or Throwable. Custom exceptions with business meaning (such as ServiceException) should be used.
  • Exceptions must be handled after they are caught (for example, logging). If you don't want to handle it, you need to throw the exception to its caller.
  • The outermost logic must handle exceptions and turn them into something that users can understand.
  • Don't Repeat Yourself, the DAY principle.

Exception handling under SpringBoot project

preparation

Step 1: create a project or module and add a web dependency. The code is as follows:

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

Step 2: modify the project access port to 80, for example

server.port=80

Step 3: define the Controller class. The code is as follows:

package com.cy.pj.arithmetic.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ArithmeticController {

  @RequestMapping("doCompute/{n1}/{n2}")
  @ResponseBody
  public String doCompute(@PathVariable  Integer n1, 
  @PathVariable Integer n2){
          Integer result=n1/n2;
          return "Result is "+result;
  }
}

Step 4 start the project for access test

Enter in the browser address bar http://localhost/doCompute/10/2 , test output results.

Result is 5

Default exception handling

Enter in the browser address bar http://localhost/doCompute/10/0 , test output results.

For such default exception handling (provided by spring boot), the user experience is not very friendly. In order to present more friendly exception information, we usually need to customize the exception handling.

Self try exception handling

In the control layer method, we can perform try catch processing, for example:

 @RequestMapping("doCompute/{n1}/{n2}")
  @ResponseBody
  public String doCompute(@PathVariable  Integer n1, 
  @PathVariable Integer n2){
          try{
          Integer result=n1/n2;
          return "Result is "+result;
          }catch(ArithmeticException e){
          return "exception is "+e.getMessage();
          }
  } 

There are usually multiple methods in a Controller class. In this way, writing try statements in multiple methods for exception handling will lead to the writing of a large number of duplicate codes and is not easy to maintain.

The exception handling method is defined inside the Controller

Add an exception handling method in the Controller class. The code is as follows:

@ExceptionHandler(ArithmeticException.class)
@ResponseBody
public String doHandleArithmeticException(ArithmeticException e){
    e.printStackTrace();
    return "An exception occurred during the calculation. The exception information is"+e.getMessage();
}

@The method described in the ExceptionHandler annotation is an exception handling method (the exception type in the annotation is a manageable exception type). If the exception is not handled after an exception occurs in the logical method in the Controller class, it will find out whether the exception handling method is defined in the Controller class. If the exception type is defined and can be handled, the exception handling method will handle the exception.

Global exception handling class and method definition in control layer

When the project is defined by multiple common exception handling methods in multiple control layer classes, we can extract these methods into the public parent class object, but this method is a strong coupling implementation, which is not conducive to code maintenance. We can also implement it with the help of the global exception handling specification defined by the web module in the spring framework. For example, we can define the global exception handling class. The code is as follows:

package com.cy.pj.common.web;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ArithmeticException.class)
    public String doHandleArithmeticException(ArithmeticException e){
        e.printStackTrace();
        return  "An exception occurred during the calculation. The exception information is"+e.getMessage();
    }
} 

Among them, the class described by @ RestControllerAdvice annotation is a global exception handling class. When the exception in the control layer method is not captured by itself or its internal exception handling method is not defined, the bottom layer will find the global exception handling class by default and call the corresponding exception handling method for exception handling.

Summary

This section mainly analyzes and explains the exception handling mechanism in springboot. The purpose is to master the exception handling method under the springboot project, and handle the response exception based on different businesses. So as to effectively improve its user experience and strengthen the fault tolerance of the system.

Tags: Spring Boot

Posted by trevHCS on Wed, 11 May 2022 07:11:14 +0300