SpringBoot 全局异常拦截捕获处理

时间:2021-07-27 20:14:02

一、全局异常处理

//Result定义全局数据返回对象
package com.xiaobing.demo001.domain; public class Result {
private Integer code;
private String message;
private Object data; public Integer getCode() {
return code;
} public String getMessage() {
return message;
} public Object getData() {
return data;
} public void setCode(Integer code) {
this.code = code;
} public void setMessage(String message) {
this.message = message;
} public void setData(Object data) {
this.data = data;
} public Result() {
} public Result(Integer code, String message, Object data) {
this.code = code;
this.message = message;
this.data = data;
} @Override
public String toString() {
return "Result{" +
"code=" + code +
", message='" + message + '\'' +
", data=" + data +
'}';
}
}

(1) RestControllerAdvice注解使用,如下全局异常示例:
注解: @RestControllerAdvice 和@ControllerAdvice 是用来修饰类的,表示为一个增强类…我们定义全局异常拦截通常是使用 @RestControllerAdvice结合 @ExceptionHandler 来捕获绝大部分异常,然后统一返回Json形式…

//假如当传参为0 时肯定会报错 除数不能为0
package com.xiaobing.demo001.controller; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("api/v1/test")
public class TestExceptionController { @GetMapping("abnormal")
public void testExt() {
int i = 1/0;
}
}
//全局异常捕获方法
package com.xiaobing.demo001.handler; import com.xiaobing.demo001.domain.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest; /**异常处理类
* @author Administrator
*/
@RestControllerAdvice
public class ExceptionsHandler { @ExceptionHandler(value = Exception.class)
Result handlerException(Exception e, HttpServletRequest request) { return new Result(500,"服务器异常","");
}
}

SpringBoot 全局异常拦截捕获处理

二、针对性异常捕获

@ExceptionHandler(value = ArithmeticException.class)
Result arithmeticExceptionException(ArithmeticException e, HttpServletRequest request) { return new Result(-1,"除数不能为0","");
}

SpringBoot 全局异常拦截捕获处理

三、自定义异常捕获

//自定义异常类
package com.xiaobing.demo001.domain; public class MyException extends RuntimeException { private String code;
private String msg; public String getCode() {
return code;
} public String getMsg() {
return msg;
} public void setCode(String code) {
this.code = code;
} public void setMsg(String msg) {
this.msg = msg;
} public MyException() {
} public MyException(String msg) {this.msg = msg;
}
}
//业务代码,MyException
@RestController
@RequestMapping("api/v1/test")
public class TestExceptionController {
@GetMapping("myException")
public void testMyExcsption() {
throw new MyException("自定义异常信息");
}
//捕获我们新增的异常
@ExceptionHandler(value = MyException.class)
Result myException(MyException e, HttpServletRequest request) {
return new Result(-1,"自定义异常信息","");
}

SpringBoot 全局异常拦截捕获处理

四、常用异常捕获分享

    /** 运行时异常 */
@ExceptionHandler(RuntimeException.class)
public Result runtimeExceptionHandler(RuntimeException ex) {
return Result.error("运行时异常");
} /** 空指针异常 */
@ExceptionHandler(NullPointerException.class)
public Result nullPointerExceptionHandler(NullPointerException ex) {
return Result.error("空指针异常");
} /** 类型转换异常 */
@ExceptionHandler(ClassCastException.class)
public Result classCastExceptionHandler(ClassCastException ex) {
return Result.error("类型转换异常");
}
/** 文件未找到异常 */
@ExceptionHandler(FileNotFoundException.class)
public Result FileNotFoundException(FileNotFoundException ex) {
return Result.error("文件未找到异常");
}
/** 数字格式异常 */
@ExceptionHandler(NumberFormatException.class)
public Result NumberFormatException(NumberFormatException ex) {
return Result.error("数字格式异常");
}
/** 安全异常 */
@ExceptionHandler(SecurityException.class)
public Result SecurityException(SecurityException ex) {
return Result.error("安全异常");
}
/** sql异常 */
@ExceptionHandler(SQLException.class)
public Result SQLException(SQLException ex) {
return Result.error("sql异常");
}
/** 类型不存在异常 */
@ExceptionHandler(TypeNotPresentException.class)
public Result TypeNotPresentException(TypeNotPresentException ex) {
return Result.error("类型不存在异常");
} /** IO异常 */
@ExceptionHandler(IOException.class)
public Result iOExceptionHandler(IOException ex) {
log.error("IO异常:{} ", ex.getMessage(), ex);
return Result.error("IO异常");
} /** 未知方法异常 */
@ExceptionHandler(NoSuchMethodException.class)
public Result noSuchMethodExceptionHandler(NoSuchMethodException ex) {
log.error("未知方法异常:{} ", ex.getMessage(), ex);
return Result.error("未知方法异常");
} /** 数组越界异常 */
@ExceptionHandler(IndexOutOfBoundsException.class)
public Result indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException ex) {
return Result.error("数组越界异常");
}
/** sql语法错误异常 */
@ExceptionHandler(BadSqlGrammarException.class)
public Result BadSqlGrammarException(BadSqlGrammarException ex) {
return Result.error("sql语法错误异常");
} /** 无法注入bean异常 */
@ExceptionHandler(NoSuchBeanDefinitionException.class)
public Result NoSuchBeanDefinitionException(NoSuchBeanDefinitionException ex) {
return Result.error("无法注入bean");
} /** Http消息不可读异常 */
@ExceptionHandler({HttpMessageNotReadableException.class})
public Result requestNotReadable(HttpMessageNotReadableException ex) {
return Result.error("Http消息不可读");
} /** 400错误 */
@ExceptionHandler({TypeMismatchException.class})
public Result requestTypeMismatch(TypeMismatchException ex) {
return Result.error("服务器异常");
} /** 500错误 */
@ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class})
public Result server500(RuntimeException ex) {
return Result.error("服务器异常");
} /** 栈溢出 */
@ExceptionHandler({*Error.class})
public Result request*(*Error ex) {
return Result.error("栈溢出异常");
} /** 除数不能为0 */
@ExceptionHandler({ArithmeticException.class})
public Result arithmeticException(ArithmeticException ex) {
return Result.error("除数不能为0异常");
} /** 其他错误 */
@ExceptionHandler({Exception.class})
public Result exception(Exception ex) {
return Result.error("网络连接失败,请退出后再试");
}