核心知识点
- @RestControllerAdvice
- @ExceptionHandler(Exception.class)
步骤
- 添加一个文件夹
expection - 添加异常处理类
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("对不起,操作失败, 请联系管理员");
}
}