SpringBoot全局捕获异常示例

//全局捕获异常对basePackages的包生效
@ControllerAdvice(basePackages = "com.wld.controller")
public class GlobalExpectionHandler(){
    
    //@ExceptionHandler(RuntimeException.class)仅仅捕获RuntimeException异常类型
    @ExceptionHandler(RuntimeException.class)
    //@ResponseBody 返回json格式
    //ModeAndView 返回页面
    @ResponseBody
    public Map<String,Object> errorResult(){
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("errorCode", "500");
        resultMap.put("errorMessage", "system Error")
        return resultMap;
    }   
}

猜你喜欢

转载自www.cnblogs.com/wldan/p/10568913.html