springboot通过aop处理抛出的异常exception
作者:互联网
写一个配置类拦截所有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 ExceptionCatch {
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseResult ExceptionHandle(Exception e) {
e.printStackTrace();
return ResponseResult.error("错误信息:"+e.getMessage()); // ResponseResult为我们自己封闭的实体类
}
}
拦截指定的异常用以下的方法(自定义的异常)
@ExceptionHandler(MyException.class)
@ResponseBody
public ResponseResult ExceptionHandle2(MyException e) {
final ResponseResult responseResult = new ResponseResult();
responseResult.setCode(e.getStatus().toString());
responseResult.setMessage(e.getMessage());
return responseResult;
}
标签:web,exception,springboot,ResponseResult,responseResult,public,aop,import,class 来源: https://www.cnblogs.com/txt1024/p/15797199.html