Springboot 全局异常捕获以及统一接口返回结果,臧萌零基础学java百度云下载

本文介绍了如何在SpringBoot中实现全局异常捕获,包括自定义BizException异常类,以及创建GlobalExceptionHandler处理各种异常,如空指针、请求方法不支持等,返回统一的ResultBody结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 响应结果

*/

private Object result;

public ResultBody() {

}

public ResultBody(BaseErrorInfoInterface errorInfo) {

this.code = errorInfo.getResultCode();

this.message = errorInfo.getResultMsg();

}

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;

}

/**

  • 成功

  • @return

*/

public static ResultBody success() {

return success(null);

}

/**

  • 成功

  • @param data

  • @return

*/

public static ResultBody success(Object data) {

ResultBody rb = new ResultBody();

rb.setCode(CommonEnum.SUCCESS.getResultCode());

rb.setMessage(CommonEnum.SUCCESS.getResultMsg());

rb.setResult(data);

return rb;

}

/**

  • 失败

*/

public static ResultBody error(BaseErrorInfoInterface errorInfo) {

ResultBody rb = new ResultBody();

rb.setCode(errorInfo.getResultCode());

rb.setMessage(errorInfo.getResultMsg());

rb.setResult(null);

return rb;

}

/**

  • 失败

*/

public static ResultBody error(String code, String message) {

ResultBody rb = new ResultBody();

rb.setCode(code);

rb.setMessage(message);

rb.setResult(null);

return rb;

}

/**

  • 失败

*/

public static ResultBody error( String message) {

ResultBody rb = new ResultBody();

rb.setCode("-1");

rb.setMessage(message);

rb.setResult(null);

return rb;

}

@Override

public String toString() {

return JSONObject.toJSONString(this);

}

}

OK,到此关于返回码的东西以及大致完毕,我们现在开始写关于异常捕捉的。

首先写一个异常类继承RuntimeException,  BizException.java:

/**

  • @Author:JCccc

  • @Description:

  • @Date: created in 15:18 2019/5/3

*/

public class BizException extends RuntimeException {

private static final long serialVersionUID = 1L;

/**

  • 错误码

*/

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;

}

public String getMessage() {

return errorMsg;

}

@Override

public Throwable fillInStackTrace() {

return this;

}

}

接着是,全局异常Handler, Glob

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

开源完整内容戳这里

alExceptionHandler.java:

/**

  • @Author:JCccc

  • @Description:

  • @Date: created in 15:29 2019/5/3

*/

@ControllerAdvice

public class GlobalExceptionHandler {

private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

/**

  • 处理自定义的业务异常

  • @param req

  • @param e

  • @return

*/

@ExceptionHandler(value = BizException.class)

@ResponseBody

public ResultBody bizExceptionHandler(HttpServletRequest req, BizException e){

logger.error(“发生业务异常!原因是:{}”,e.getErrorMsg());

return ResultBody.error(e.getErrorCode(),e.getErrorMsg());

}

/**

  • 处理空指针的异常

  • @param req

  • @param e

  • @return

*/

@ExceptionHandler(value =NullPointerException.class)

@ResponseBody

public ResultBody exceptionHandler(HttpServletRequest req, NullPointerException e){

logger.error(“发生空指针异常!原因是:”,e);

return ResultBody.error(CommonEnum.BODY_NOT_MATCH);

}

/**

  • 处理请求方法不支持的异常

  • @param req

  • @param e

  • @return

*/

@ExceptionHandler(value =HttpRequestMethodNotSupportedException.class)

@ResponseBody

public ResultBody exceptionHandler(HttpServletRequest req, HttpRequestMethodNotSupportedException e){

logger.error(“发生请求方法不支持异常!原因是:”,e);

return ResultBody.error(CommonEnum.REQUEST_METHOD_SUPPORT_ERROR);

}

/**

  • 处理其他异常

  • @param req

  • @param e

  • @return

*/

@ExceptionHandler(value =Exception.class)

@ResponseBody

public ResultBody exceptionHandler(HttpServletRequest req, Exception e){

logger.error(“未知异常!原因是:”,e);

return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);

}

}

这里可以手动配置自己想抓取的异常以及处理返回码。

接着写个Controller来模拟一下异常抛出,抓取,使用统一返回码,UserController.java:

/**

  • @Author:JCccc

  • @Description:

  • @Date: created in 15:50 2019/5/20

*/

@RestController

@RequestMapping("/user")

public class UserController {

@GetMapping("/test")

public boolean test() {

System.out.println(“开始…”);

//这里故意造成一个异常,并且不进行处理

Integer.parseInt(“abc123”);

return true;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值