首页 > 其他分享 >SpringBoot Controller全局异常捕获

SpringBoot Controller全局异常捕获

时间:2022-09-27 16:55:26浏览次数:69  
标签:SpringBoot 捕获 Object HttpServletResponse Controller result 全局 异常 Description

/**
 * <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;
    }
}

标签:SpringBoot,捕获,Object,HttpServletResponse,Controller,result,全局,异常,Description
From: https://www.cnblogs.com/IamHzc/p/16735102.html

相关文章