当前位置:网站首页>3-全局异常处理
3-全局异常处理
2022-07-29 06:52:00 【张 邵】
异常处理(SpringMVC知识)
了解异常
SpringMVC 注解方式处理
该处理方式是在 核心控制器加载的时候就加载了,所以可以拦截到参数中的异常
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@Component
//使用注解开发异常处理器
//声明该类是一个Controller的通知类,声明后该类就会被加载成异常处理器
//可以在controller的前后做功能增强
@ControllerAdvice
public class ExceptionAdvice {
//类中定义的方法携带@ExceptionHandler注解的会被作为异常处理器,后面添加实际处理的异常类型
@ExceptionHandler(NullPointerException.class)
@ResponseBody
public String doNullException(Exception ex){
return "空指针异常";
}
@ExceptionHandler(ArithmeticException.class)
@ResponseBody
public String doArithmeticException(Exception ex){
return "ArithmeticException";
}
@ExceptionHandler(Exception.class)
@ResponseBody
public String doException(Exception ex){
return "all";
}
}
项目异常处理方案【理解】
自定义异常
业务异常(用户) BusinessException
//自定义异常继承RuntimeException,覆盖父类所有的构造方法
public class BusinessException extends RuntimeException {
public BusinessException() {
}
public BusinessException(String message) {
super(message);
}
public BusinessException(String message, Throwable cause) {
super(message, cause);
}
public BusinessException(Throwable cause) {
super(cause);
}
public BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
系统异常: SystemException
//自定义异常继承RuntimeException,覆盖父类所有的构造方法
public class SystemException extends RuntimeException {
public SystemException() {
}
public SystemException(String message) {
super(message);
}
public SystemException(String message, Throwable cause) {
super(message, cause);
}
public SystemException(Throwable cause) {
super(cause);
}
public SystemException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
其他异常: DevelopException
//自定义异常继承RuntimeException,覆盖父类所有的构造方法
public class DevelopException extends RuntimeException {
public DevelopException() {
}
public DevelopException(String message) {
super(message);
}
public DevelopException(String message, Throwable cause) {
super(message, cause);
}
public DevelopException(Throwable cause) {
super(cause);
}
public DevelopException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
将异常包装为自定义异常
public void save(){
//业务层中如果出现了异常,就对出现异常的代码进行try...catch...处理
//在catch中将出现的异常包装成自定义异常,同时务必将当前异常对象传入自定义异常,避免真正的异常信息消失
try {
throw new SQLException();
} catch (SQLException e) {
throw new SystemException("数据库连接超时!",e);
}
}
统一处理异常
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@Component
@ControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(BusinessException.class)
@ResponseBody()
public String doBusinessException(Exception ex, Model m){
//使用参数Model将要保存的数据传递到页面上,功能等同于ModelAndView
//业务异常出现的消息要发送给用户查看
// m.addAttribute("msg",ex.getMessage());
return ex.getMessage();
}
@ExceptionHandler(value = {
SystemException.class})
@ResponseBody
public String doSystemException(Exception ex, Model m){
//系统异常出现的消息不要发送给用户查看,发送统一的信息给用户看
// m.addAttribute("msg","服务器出现问题,请联系管理员!");
//实际的问题现象应该传递给redis服务器,运维人员通过后台系统查看
//实际的问题显现更应该传递给redis服务器,运维人员通过后台系统查看
return "服务器出现问题,请联系管理员!";
}
@ExceptionHandler(Exception.class)
@ResponseBody
public String doException(Exception ex, Model m){
// m.addAttribute("msg",ex.getMessage());
//将ex对象保存起来
return ex.getMessage();
}
}
统一异常处理
@Component
@ControllerAdvice
@Log4j2
public class ProjectExceptionAdvice {
@ExceptionHandler(BusinessException.class)
@ResponseBody()
public ResponseResult doBusinessException(Exception exception, Model m){
//使用参数Model将要保存的数据传递到页面上,功能等同于ModelAndView
//业务异常出现的消息要发送给用户查看
// m.addAttribute("msg",ex.getMessage());
// return ex.getMessage();
log.error("catch exception:{}", exception.getMessage());
return ResponseResult.errorResult(500,exception.getMessage());
}
@ExceptionHandler(value = {
SystemException.class})
@ResponseBody
public ResponseResult doSystemException(Exception exception, Model m){
//系统异常出现的消息不要发送给用户查看,发送统一的信息给用户看
// m.addAttribute("msg","服务器出现问题,请联系管理员!");
//实际的问题现象应该传递给redis服务器,运维人员通过后台系统查看
//实际的问题显现更应该传递给redis服务器,运维人员通过后台系统查看
// return "服务器出现问题,请联系管理员!";
log.error("catch exception:{}", exception.getMessage());
return ResponseResult.errorResult(503,"服务器出现问题,请联系管理员!");
}
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseResult doException(Exception exception, Model m){
// m.addAttribute("msg",ex.getMessage());
//将ex对象保存起来
//记录日志
exception.printStackTrace();
log.error("catch exception:{}", exception.getMessage());
return ResponseResult.errorResultMessage(AppHttpCodeEnum.SERVER_ERROR,exception.getMessage());
}
}
边栏推荐
猜你喜欢
js中break与continue和return关键字
接口测试实战项目03:执行测试用例
彻底搞懂kubernetes调度框架与插件
Implementation of book borrowing management system based on C language
[OpenGL] use of shaders
MySQL 使用客户端以及SELECT 方式查看 BLOB 类型字段内容总结
微服务远程调用
OCR光学字符识别方法汇总
Problems encountered in vmware16 installing virtual machines
Variables and encryption in ansible
随机推荐
NPM install reports an error NPM err could not resolve dependency NPM err peer
win11系统错误:由于找不到 iertutil.dll,无法继续执行代码。重新安装程序可能会解决此问题
pytorch的技巧记录
个人博客系统(附源码)
Flink real-time warehouse DWD layer (processing complex data - installation and replacement of streams and tables) template code
Operator3-设计一个operator
对Vintage分析的一些学习理解
Variables and encryption in ansible
Flink real-time warehouse DWD layer (order placing multiple tables to realize join operation) template code
SQL优化
聊天机器人有何用处?有何类型?看完这些就明白了!
SpingBoot整合Quartz框架实现动态定时任务(支持实时增删改查任务)
Redis基础篇
gin 模版
2022-07-28:以下go语言代码输出什么?A:AA;B:AB;C:BA;D:BB。 package main import ( “fmt“ ) func main() { f
如何使用gs_expansion扩展节点
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
Revolution of game assets
5-整合swagger2
Cvpr2021 | multi view stereo matching based on self supervised learning (cvpr2021)