如何在SpringBoot中实现全局异常处理方式?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。springboot是什么springboot一种全新的编程规范,
如何在SpringBoot中实现全局异常处理方式?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。
SpringBoot中,@ControllerAdvice 即可开启全局异常处理,使用该注解表示开启了全局异常的捕获,我们只需在自定义一个方法使用@ExceptionHandler注解然后定义捕获异常的类型即可对这些捕获的异常进行统一的处理。
@ControllerAdvicepublic class MyExceptionHandler { @ExceptionHandler(value =Exception.class) @ResponseBody public String exceptionHandler(Exception e){ System.out.println("全局异常捕获>>>:"+e); return "全局异常捕获,错误原因>>>"+e.getMessage(); }}
@GetMapping("/getById/{userId}")public CommonResult<User> getById(@PathVariable Integer userId){ // 手动抛出异常 int a = 10/0; return CommonResult.success(userService.getById(userId));}
很显然这样的用户体验效果是极差的,虽然这种能够让我们知道异常的原因,但是在很多的情况下来说,可能还是不够人性化,不符合我们的要求。
public interface BaseErrorInfoInterface { String getResultCode(); String getResultMsg();}
public enum ExceptionEnum implements BaseErrorInfoInterface{ // 数据操作错误定义 SUCCESS("2000", "成功!"), BODY_NOT_MATCH("4000","请求的数据格式不符!"), SIGNATURE_NOT_MATCH("4001","请求的数字签名不匹配!"), NOT_FOUND("4004", "未找到该资源!"), 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 resultCode; } @Override public String getResultMsg() { return resultMsg; }}
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; } @Override public Throwable fillInStackTrace() { return this; }}
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 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; } 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(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); }}
@ControllerAdvicepublic class GlobalExceptionHandler { private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); @ExceptionHandler(value = BizException.class) @ResponseBody public ResultResponse bizExceptionHandler(httpservletRequest req, BizException e){ logger.error("发生业务异常!原因是:{}",e.getErrorMsg()); return ResultResponse.error(e.getErrorCode(),e.getErrorMsg()); } @ExceptionHandler(value =NullPointerException.class) @ResponseBody public ResultResponse exceptionHandler(HttpServletRequest req, NullPointerException e){ logger.error("发生空指针异常!原因是:",e); return ResultResponse.error(ExceptionEnum.BODY_NOT_MATCH); } @ExceptionHandler(value =Exception.class) @ResponseBody public ResultResponse exceptionHandler(HttpServletRequest req, Exception e){ logger.error("未知异常!原因是:",e); return ResultResponse.error(ExceptionEnum.INTERNAL_SERVER_ERROR); }}
@PostMapping("/add")public boolean add(@RequestBody User user) { //如果姓名为空就手动抛出一个自定义的异常! if(user.getName()==null){ throw new BizException("-1","用户姓名不能为空!"); } return true;}
@PutMapping("/update")public boolean update(@RequestBody User user) { //这里故意造成一个空指针的异常,并且不进行处理 String str = null; str.equals("111"); return true;}
@DeleteMapping("/delete")public boolean delete(@RequestBody User user) { //这里故意造成一个异常,并且不进行处理 Integer.parseInt("abc123"); return true;}
如果我们想捕获这个类型转换异常,是不是再添加一个遗产处理方法就可了。
@ExceptionHandler(value = NumberFormatException.class)@ResponseBodypublic ResultResponse exceptionHandler(HttpServletRequest req, NumberFormatException e){ logger.error("发生类型转换异常!原因是:",e); return ResultResponse.error(ExceptionEnum.PARAMS_NOT_CONVERT);}
PARAMS_NOT_CONVERT("4002","类型转换不对!"),
自定义全局异常处理除了可以处理上述的数据格式之外,也可以处理页面的跳转,只需在新增的异常方法的返回处理上填写该跳转的路径并不使用ResponseBody 注解即可。
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注编程网精选频道,感谢您对编程网的支持。
--结束END--
本文标题: 如何在SpringBoot中实现全局异常处理方式
本文链接: https://www.lsjlt.com/news/274161.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-13
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0