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"; }