建立全局异常包
/**
* 全局异常处理
*/
拦截类上加了RestController的controller和普通的controller
@ControllerAdvice(annotations = {RestController.class, Controller.class})
最后需要把结果封装成json数据
@ResponseBody
添加日志
@Slf4j
public class GlobalExceptionHandler {
/**
* 异常处理方法
* @return
*/
@ExceptionHandler(SQLIntegrityConstraintViolationException.class)
public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
log.error(ex.getMessage());
if(ex.getMessage().contains("Duplicate entry")){
String[] split = ex.getMessage().split(" ");
String msg = split[2] + "已存在";
return R.error(msg);
}
return R.error("未知错误");
}
}
![](https://img-blog.csdnimg.cn/img_convert/8c23b16b4ac4d17f8669479ca874af1f.png)
![](https://img-blog.csdnimg.cn/img_convert/24470d93b1529015eac7b73bd2441f64.png)
![](https://img-blog.csdnimg.cn/img_convert/5452cb577895e85ed11b9cecb20e0573.png)
DigestUtils可以用到:明文密码加密MD5
![](https://img-blog.csdnimg.cn/img_convert/cf1c1177fd09e144593e7fb729ec0488.png)