Loading

SpringBoot Controller全局异常捕获

/**
 * <P><B>Description: </B>全局异常处理</P>
 * Revision Trail: (Date/Author/Description)
 * 2022/9/27 Ryan Huang CREATE
 *
 * @author Ryan Huang
 * @version 1.0
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Object customerExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e){
        Object result;
        if(e instanceof RuntimeException){
            //异常处理
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            result = "异常操作1";
        }else{
            result = "异常操作2";
        }
        return result;
    }
}
posted @ 2022-09-27 16:43  IamHzc  阅读(41)  评论(0编辑  收藏  举报