栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot 全局异常捕获以及统一接口返回结果(1),java常用技术框架

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot 全局异常捕获以及统一接口返回结果(1),java常用技术框架

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;

}

public static ResultBody success() {

return success(null);

}

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:

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, GlobalExceptionHandler.java:

@ControllerAdvice

public class GlobalExceptionHandler {

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

@ExceptionHandler(value = BizException.class)

@ResponseBody

public ResultBody bizExceptionHandl

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

开源完整内容戳这里

er(HttpServletRequest req, BizException e){

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

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

}

@ExceptionHandler(value =NullPointerException.class)

@ResponseBody

public ResultBody exceptionHandler(HttpServletRequest req, NullPointerException e){

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

return ResultBody.error(CommonEnum.BODY_NOT_MATCH);

}

@ExceptionHandler(value =HttpRequestMethodNotSupportedException.class)

@ResponseBody

public ResultBody exceptionHandler(HttpServletRequest req, HttpRequestMethodNotSupportedException e){

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

return ResultBody.error(CommonEnum.REQUEST_METHOD_SUPPORT_ERROR);

}

@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:

@RestController

@RequestMapping("/user")

public class UserController {

@GetMapping("/test")

public boolean test() {

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

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

Integer.parseInt(“abc123”);

return true;

}

@GetMapping("/testNull")

public boolean testNull() {

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

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

String str=null;

str.equals(“111”);

return true;

}

@PostMapping("/testBizException")

public boolean testBizException() {

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

//如果姓名为空就手动抛出一个自定义的异常!

String userName=null;

if(userName==null){

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/531012.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号