Unified exception handling in Spring boot

When using spring boot to develop a Java Web project, it is inevitable to encounter exceptions when processing requests submitted by users at the Controller layer. Therefore, it is necessary to learn to deal with exceptions in the Controller layer uniformly.

project download


Unified exception handling:

  1. global exception handling
  2. specific exception handling
  3. Custom exception handling

main code

Test Controller

package top.leeti.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.leeti.entity.R;
import top.leeti.exception.MyException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class IndexController {
    
    

    @ApiOperation("正常测试的 uri")
    @GetMapping("test")
    public R test() {
    
    
        return R.ok();
    }

    @ApiOperation("测试全局异常的 uri")
    @GetMapping("globalError")
    public R testGlobalError() {
    
    
        // 抛出: IndexOutOfBoundsException
        // 并被全局异常处理器捕获
        List<String> list = new ArrayList<>(Arrays.asList("a"));
        list.get(3);

        return R.ok();
    }

    @ApiOperation("测试特定异常的 uri")
    @GetMapping("specialError")
    public R testSpecialError() {
    
    
        // 抛出: ArithmeticException
        // 并被 ArithmeticException 异常处理器捕获
        int ans = 9 / 0;

        return R.ok();
    }

    @ApiOperation("测试自定义异常的 uri")
    @GetMapping("mineError")
    public R testMineError() throws MyException {
    
    
        // 抛出: MyException
        // 并被 MyException 异常处理器捕获
        throw new MyException();
    }
}

Tested exception handler

package top.leeti.handler;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import top.leeti.entity.R;
import top.leeti.exception.MyException;

@RestControllerAdvice
public class TestExceptionHandler {
    
    

    @ExceptionHandler(Exception.class)
    public R error(Exception e) {
    
    
        e.printStackTrace();
        return R.error().message("执行出现了全局异常处理......");
    }

    @ExceptionHandler(ArithmeticException.class)
    public R error(ArithmeticException e) {
    
    
        e.printStackTrace();
        return R.error().message("执行出现了 ArithmeticException 异常处理......");
    }

    @ExceptionHandler(MyException.class)
    public R error(MyException e) {
    
    
        e.printStackTrace();
        return R.error().message("执行出现了 MyException 异常处理......");
    }
}

start testing

Startup project

Enter the url in turn, and observe the returned results:

  • http://localhost:8000/test
    insert image description here
  • http://localhost:8000/globalError
    insert image description here
  • http://localhost:8000/specialError
    insert image description here
  • http://localhost:8000/mineError
    insert image description here

So far, the unified exception handling in Spring boot is over.

It should be noted:

The principle of unified exception handling is spring's () aspect-oriented programming.
Global exceptions can match all exceptions without special instructions (that is, exceptions that are handled separately).

Guess you like

Origin blog.csdn.net/Snakehj/article/details/113712326