SpringBoot捕获全局异常

1、创建GloableExceptionAop类捕获全局异常

package com.cppdy.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GloableExceptionAop {
    
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public String runtimeException() {
        return "出现异常了,这里在捕获全局异常,相当于手写AOP捕获异常";
    }

}

2、在HelloWordController类中创建测试方法

@RequestMapping("excep")
    public String excep() {

        int a=2/0;
        
        return "Hello Exception";
    }

 

posted @ 2018-12-01 00:32  知识追求者  阅读(151)  评论(0编辑  收藏  举报