springboot统一异常处理
统一异常处理,能够将系统中出现的异常统一被工具类拦截下来,实现集中管理。
制造异常
// @TableField(value = "is_deleted")
private Boolean deleted;
创建异常处理工具类
guli-framework-common中的com.guli.common.util包中,
创建异常处理工具类ExceptionUtil.java:
public class ExceptionUtil {
public static String getMessage(Exception e) {
StringWriter sw = null;
PrintWriter pw = null;
try {
sw = new StringWriter();
pw = new PrintWriter(sw);
// 将出错的栈信息输出到printWriter中
e.printStackTrace(pw);
pw.flush();
sw.flush();
} finally {
if (sw != null) {
try {
sw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (pw != null) {
pw.close();
}
}
return sw.toString();
}
}
一、统一异常处理
1、创建统一异常处理器
guli-framework-common中的com.guli.common.handler包中,
创建统一异常处理类GlobalExceptionHandler.java:
package com.guli.common.handler;
/**
* 统一异常处理类
*/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
public R error(Exception e){
//打印异常
// e.printStackTrace();
//log.error(e.getMessage());
log.error(ExceptionUtil.getMessage(e));
//将R.error()方法返回给前端
return R.error();
}
/**
* sql语句出问题
* @param e
* @return
*/
@ExceptionHandler(BadSqlGrammarException.class)
@ResponseBody
public R error(BadSqlGrammarException e){
// e.printStackTrace();
log.error(ExceptionUtil.getMessage(e));
return R.setResult(ResultCodeEnum.BAD_SQL_GRAMMAR);
}
/**
* 前端传来json数据不符合规范
* @param e
* @return
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody
public R error(JsonParseException e){
// e.printStackTrace();
log.error(ExceptionUtil.getMessage(e));
return R.setResult(ResultCodeEnum.JSON_PARSE_ERROR);
}
@ExceptionHandler(GuliException.class)
@ResponseBody
public R error(GuliException e){
// e.printStackTrace();
log.error(ExceptionUtil.getMessage(e));
return R.error().message(e.getMessage()).code(e.getCode());
}
}
2、扫描异常处理器
@SpringBootApplication
@ComponentScan(basePackages={"com.guli.edu","com.guli.common"})
public class EduApplication {
3、恢复制造的异常
@TableField(value = "is_deleted")
private Boolean deleted;
二、自定义异常
1、创建自定义异常类
common模块中创建com.guli.common.exception包,
创建GuliException.java通用异常类 继承 RuntimeException
RuntimeException 对代码没有侵入性
package com.guli.common.exception;
@Data
@ApiModel(value = "全局异常")
public class GuliException extends RuntimeException {
@ApiModelProperty(value = "状态码")
private Integer code;
/**
* 接受状态码和消息
* @param code
* @param message
*/
public GuliException(Integer code, String message) {
super(message);
this.code=code;
}
/**
* 接收枚举类型
* @param resultCodeEnum
*/
public GuliException(ResultCodeEnum resultCodeEnum) {
super(resultCodeEnum.getMessage());
this.code = resultCodeEnum.getCode();
}
@Override
public String toString() {
return "GuliException{" +
"code=" + code +
",message=" + this.getMessage() +
'}';
}
}
该自定义异常类能够使我能够在需要拦截异常的地方声明拦截异常。
2、业务中需要的位置抛出GuliException
@ExceptionHandler(GuliException.class)
@ResponseBody
public R error(GuliException e){
e.printStackTrace();
return R.error().message(e.getMessage()).code(e.getCode());
}
e.getCode()是获取状态码。
总结
有了统一异常处理之后,我们能够从容的面对系统中出现的异常,并能很快的定位到产生异常的位置。希望这篇文章能对你有小小的帮助,感谢你的阅读。我是黑马Jack,一起学习一起进步!