[Java2023] Day12.16-全局异常处理

48 阅读1分钟
核心知识点
  • @RestControllerAdvice
  • @ExceptionHandler(Exception.class)
步骤
  1. 添加一个文件夹 expection
  2. 添加异常处理类
package com.tlias.expection;

import com.tlias.pojo.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result ex(Exception exObj) {
        exObj.printStackTrace();
        return Result.error("对不起,操作失败, 请联系管理员");
    }
}

image.png