1. 实现思路
通过RestControllerAdvice 实现全局异常处理
@RestControllerAdvice主要有3个功能:
- 捕获全局异常: 能够捕获controller 中抛出的制定类型的异常,从而进行指定的处理
- 进行数据绑定
- 全局数据预处理
2. 创建返回格式及异常类
2.1 创建异常处理接口
/**
* @author ml
*/
public interface BaseErrorInfoInterface {
/**
* 获取错误码
* @return 错误码
*/
String getResultCode();
/**
* 获取错误信息
* @return 错误信息
*/
String getResultMsg();
}
2.2 创建自定义异常类
/**
* 自定义异常类
* @author ml
*/
public class BizException extends RuntimeException{
private static final long serialVersionUID = 1350751357838080111L;
/**
* 错误码
*/
protected String errorCode;
/**
* 错误信息
*/
protected String errorMsg;
public BizException() {
super();
}
public BizException(BaseErrorInfoInterface errorInfoInterface) {
super(errorInfoInterface.getResultCode());
this.errorCode = errorInfoInterface.getResultCode();
this.errorMsg = errorInfoInterface.getResultMsg();
}
public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
super(errorInfoInterface.getResultCode(), cause);
this.errorCode = errorInfoInterface.getResultCode();
this.errorMsg = errorInfoInterface.getResultMsg();
}
public BizException(String errorMsg) {
super(errorMsg);
this.errorMsg = errorMsg;
}
public BizException(String errorCode, String errorMsg) {
super(errorCode);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public BizException(String errorCode, String errorMsg, Throwable cause) {
super(errorCode, cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
@Override
public Throwable fillInStackTrace() {
return this;
}
}
2.3 创建自定义异常枚举
/**
* 自定义异常枚举
* @author ml
*/
public enum ExceptionEnum implements BaseErrorInfoInterface {
// 数据操作错误定义
SUCCESS("2000", "成功!"),
BODY_NOT_MATCH("4000", "请求的数据格式不符!"),
INTERNAL_SERVER_ERROR("5000", "服务器内部错误!"),
SERVER_BUSY("5003", "服务器正忙,请稍后再试!");
private final String resultCode;
private final String resultMsg;
ExceptionEnum(String resultCode, String resultMsg) {
this.resultCode = resultCode;
this.resultMsg = resultMsg;
}
@Override
public String getResultCode() {
return this.resultCode;
}
@Override
public String getResultMsg() {
return this.resultMsg;
}
}
2.4 自定义数据返回结构
/**
* 自定义数据返回结构
*
* @author ml
*/
public class ResultResponse {
/**
* 响应代码
*/
private String code;
/**
* 响应消息
*/
private String message;
/**
* 响应结果
*/
private Object result;
public ResultResponse() {
}
public ResultResponse(BaseErrorInfoInterface errorInfo) {
this.code = errorInfo.getResultCode();
this.message = errorInfo.getResultMsg();
}
/**
* 成功
*/
public static ResultResponse success() {
return success(null);
}
/**
* 成功
*/
public static ResultResponse success(Object data) {
ResultResponse rb = new ResultResponse();
rb.setCode(ExceptionEnum.SUCCESS.getResultCode());
rb.setMessage(ExceptionEnum.SUCCESS.getResultMsg());
rb.setResult(data);
return rb;
}
/**
* 失败
*/
public static ResultResponse error(BaseErrorInfoInterface errorInfo) {
ResultResponse rb = new ResultResponse();
rb.setCode(errorInfo.getResultCode());
rb.setMessage(errorInfo.getResultMsg());
rb.setResult(null);
return rb;
}
/**
* 失败
*/
public static ResultResponse error(BizException errorInfo) {
ResultResponse rb = new ResultResponse();
rb.setCode(errorInfo.getErrorCode());
rb.setMessage(errorInfo.getErrorMsg());
rb.setResult(null);
return rb;
}
/**
* 失败
*/
public static ResultResponse error(String code, String message) {
ResultResponse rb = new ResultResponse();
rb.setCode(code);
rb.setMessage(message);
rb.setResult(null);
return rb;
}
/**
* 失败
*/
public static ResultResponse error(String message) {
ResultResponse rb = new ResultResponse();
rb.setCode("-1");
rb.setMessage(message);
rb.setResult(null);
return rb;
}
@Override
public String toString() {
return JSONObject.toJSONString(this);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}
3. 创建全局异常处理类
/**
* 全局异常处理
* @author ml
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 自定义异常
* @param req request
* @param e exception
*/
@ExceptionHandler(value = BizException.class)
public ResultResponse bizExceptionHandler(HttpServletRequest req, BizException e){
logger.error("发生业务异常!原因是:{}",e.getErrorMsg());
return ResultResponse.error(e);
}
/**
* 处理空指针的异常
*/
@ExceptionHandler(value =NullPointerException.class)
public ResultResponse exceptionHandler(HttpServletRequest req, NullPointerException e){
logger.error("发生空指针异常!原因是:",e);
return ResultResponse.error(ExceptionEnum.BODY_NOT_MATCH);
}
/**
* 处理其他异常
*/
@ExceptionHandler(value =Exception.class)
public ResultResponse exceptionHandler(HttpServletRequest req, Exception e){
logger.error("未知异常!原因是:",e);
return ResultResponse.error(ExceptionEnum.INTERNAL_SERVER_ERROR);
}
}
4. 创建测试demoController
/**
* @author ml
*/
@RestController
@RequestMapping("/demo")
public class DemoController {
/**
* 抛出自定义异常
*/
@RequestMapping("/demo")
public String demo(){
throw new BizException("-1","aaa");
}
/**
* 抛出特定类型异常 空指针
*/
@RequestMapping("/demo1")
public String demo1(){
String a = null;
a.equals("333");
return "";
}
}