自定义全局异常
bizException基础包下封装异常类
BizException:运行时业务中出现的异常
/**
* @Description : 运行时业务中出现的异常
* @Param :
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
public class BizException extends RuntimeException {
private static final long serialVersionUID = -7864604160297181941L;
private final String code;
/**
* @Description : 指定枚举类中的错误类
* @Param : [errorCode]
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
public BizException(final BizExceptionCode exceptionCode) {
super(exceptionCode.getMessage());
this.code = exceptionCode.getCode();
}
/**
* @Description : 指定具体业务错误的信息
* @Param : [detailedMessage]
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
public BizException(final String message) {
super(message);
this.code = BizExceptionCodeEnum.SPECIFIED.getCode();
}
public String getCode() {
return code;
}
}
BizExceptionCode: 业务异常的错误代码接口
/**
* @Description : 业务异常的错误代码接口
* @Param :
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
public interface BizExceptionCode {
/**
* @Description : 获取错误代码
* @Param : []
* @Return : java.lang.String
* @Author : l-jiahui
* @Date : 2020-10-11
*/
String getCode();
/**
* @Description : 获取错误信息
* @Param : []
* @Return : java.lang.String
* @Author : l-jiahui
* @Date : 2020-10-11
*/
String getMessage();
}
BizExceptionCodeEnum:异常消息的枚举类(此类属于业务异常枚举类)
/**
* @Description : 异常消息的枚举类(此类属于业务异常枚举类)
* @Param :
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
public enum BizExceptionCodeEnum implements BizExceptionCode{
// 已指明的异常,在异常使用时message并不返回前端,返回前端的为throw新的异常时指定的message
SPECIFIED("-1","系统发生异常,请稍后重试"),
// 常用业务异常
USER_NAME_NULL("-1","用户名不能为空,请重新输入!"),
USER_PASSWORD_NULL("-1","密码不能为空,请重新输入!"),
USER_PASSWORD_WRONG("-1","密码错误,请检查后重新输入!"),
PAGE_NUM_NULL("4001","页码不能为空"),
PAGE_SIZE_NULL("4002","页数不能为空"),
SEARCH_NULL("4004","搜索条件不能为空,请检查后重新输入!"),
NO_LOGIN("3001", "用户未进行登录")
;
private final String code;
private final String message;
/**
* @Description :
* @Param : [code, message]
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
BizExceptionCodeEnum(String code,String message){
this.code = code;
this.message = message;
}
@Override
public String getCode() {
return code;
}
@Override
public String getMessage() {
return message;
}
}
全局捕获异常和自定义全局捕获异常以及404处理
/**
* @author l-jiahui
* extend of {@link ResponseEntityExceptionHandler} for handle all exception
* @Description: 全局捕获异常和自定义全局捕获异常
*/
@ControllerAdvice
public class GlobalControllerAdvice {
/**
* 拦截捕捉自定义异常 BizException.class
*
* @param bizException 自定义异常
* @return map
*/
@ResponseBody
@ExceptionHandler(value = BizException.class)
public Map<String, Object> myExceptionHandler(BizException bizException, HttpServletResponse response) {
Map<String, Object> map = new HashMap<>(16);
map.put("code", bizException.getCode());
map.put("msg", bizException.getMessage());
response.setStatus(Integer.parseInt(bizException.getCode()));
return map;
}
/**
* 增加处理404的统一错误信息
*
* @param response response对象
* @return 返回map对应的对象信息
*/
@ExceptionHandler(value = {NoHandlerFoundException.class})
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
public Map<String, Object> notFoundException(HttpServletResponse response) {
Map<String, Object> map = new HashMap<>(16);
map.put("code", "404");
map.put("msg", "not found exception");
response.setStatus(404);
return map;
}
}