Spring Boot统一接口返回及全局异常处理的方法
发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,这篇文章主要讲解了"Spring Boot统一接口返回及全局异常处理的方法",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Spring Boot统一接口
千家信息网最后更新 2025年02月01日Spring Boot统一接口返回及全局异常处理的方法
这篇文章主要讲解了"Spring Boot统一接口返回及全局异常处理的方法",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Spring Boot统一接口返回及全局异常处理的方法"吧!
1、解决方案
定义公共模块,实现统一接口定义规范和异常处理,其他的系统进行依赖和扩展即可。
2、具体实现
2.1 定义状态码统一接口
public interface BaseResultCode{ /** * 状态码 * @return */ int getCode(); /** * 提示信息 * @return */ String getMsg();}
2.2 公共模块状态码枚举类
public enum ResultCode implements BaseResultCode{ OK(200, "成功"), ERROR(300,"系统异常"), NEED_AUTH(301, "非法请求,请重新登录"), PARAMTER_ERROR(302, "参数错误"); //省略其他定义错误码 private int code; private String msg; private ResultCode(int code, String msg) { this.code = code; this.msg = msg; } public static ResultCode getValue(int code) { for (ResultCode errorCode : values()) { if (errorCode.getCode() == code) { return errorCode; } } return null; } //省略Get、Set方法 }
2.3 定义全局自定义异常
public class SysException extends RuntimeException{ private static final long serialVersionUID = 5225171867523879342L; private int code; private String msg; private Object[] params; private BaseResultCode errorCode; public SysException() { super(); } public SysException(String message) { super(message); } public SysException(Throwable cause) { super(cause); } public SysException(int code ,String message) { this.code = code; this.msg = message; } public SysException(int code ,String message, Object[] params) { this(code, message); this.params= params; } public SysException(String message, Throwable cause) { super(message, cause); } public SysException(BaseResultCode errorCode) { this.errorCode = errorCode; } public SysException(String message, Object[] params) { super(message); this.params = params; } public SysException(BaseResultCode errorCode, String message, Object[] params) { this(message, params); this.errorCode = errorCode; } /** * Construct by default * * @param message * message * @param parameters * parameters * @param cause * cause */ public SysException(String message, Object[] params, Throwable cause) { super(message, cause); this.params = params; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } /** * @return the params */ public Object[] getParams() { return params; } /** * @param params * the params to set */ public void setParams(Object[] params) { this.params = params; } public BaseResultCode getErrorCode() { return errorCode; } public void setErrorCode(BaseResultCode errorCode) { this.errorCode = errorCode; } }
2.4 定义统一接口格式输出类
public class Result implements Serializable{ private static final long serialVersionUID = -1773941471021475043L; private Object data; private int code; private String msg; public Result() { } public Result(int code, Object data, String msg) { this.code = code; this.data = data; this.msg = msg; } public Result(int code, String desc) { this(code, null, desc); } public Result(BaseResultCode errorCode) { this(errorCode.getCode(), null, errorCode.getMsg()); } public static Result success() { return success(null); } public static Result success(Object data) { Result result = new Result(); result.setData(data); result.setCode(ResultCode.OK.getCode()); return result; } public static Result error(String msg) { Result result = new Result(); result.setCode(ResultCode.ERROR.getCode()); result.setMsg(msg); return result; } public static Result error(BaseResultCode baseCode) { Result result = new Result(); result.setCode(baseCode.getCode()); result.setMsg(baseCode.getMsg()); return result; }}
个人建议:统一接口输出类不要定义为泛型类型
2.5 定义统一接口格式输出类
@RestControllerAdvicepublic class SysExceptionHandler{ public static Log logger = LogManager.getLogger(SysExceptionHandler.class); @ExceptionHandler(Exception.class) public Result handleException(HttpServletRequest request, Exception ex) { logger.error("Handle Exception Request Url:{},Exception:{}",request.getRequestURL(),ex); Result result = new Result(); //系统异常 if (ex instanceof SysException) { SysException se = (SysException) ex; BaseResultCode resultCode =se.getErrorCode(); if(resultCode==null) { result = Result.error(se.getMessage()); } else { result = new Result(resultCode.getCode(), StringUtil.isNotEmpty(se.getMessage())?se.getMessage():resultCode.getMsg()); } } //参数错误 else if (ex instanceof ConstraintViolationException) { ConstraintViolationException v = (ConstraintViolationException) ex; String message = v.getConstraintViolations().iterator().next() .getMessage(); result.setCode(ResultCode.PARAMTER_ERROR.getCode()); result.setMsg(ResultCode.PARAMTER_ERROR.getMsg() + ":" + message); } //参数错误 else if (ex instanceof BindException) { BindException v = (BindException) ex; String message = v.getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(",")); result.setCode(ResultCode.PARAMTER_ERROR.getCode()); result.setMsg(ResultCode.PARAMTER_ERROR.getMsg() + ":" + message); } //参数错误 else if (ex instanceof MethodArgumentNotValidException) { MethodArgumentNotValidException v = (MethodArgumentNotValidException) ex; String message = v.getBindingResult().getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(",")); result.setCode(ResultCode.PARAMTER_ERROR.getCode()); result.setMsg(ResultCode.PARAMTER_ERROR.getMsg() + ":" + message); } else { result = new Result(ResultCode.ERROR.getCode(),ExceptionUtil.getErrorMsg(ex)); } logger.info("exception handle reuslt:" + result); return result; }}
上述定义已经可以实现全局接口和异常的统一处理,但是存在的如下问题
每个controller
都需要返回Reesult类型,且每个方法都需要返回Result.success()
或者Result.success(data)
的结果,有点重复,需要进行优化。
@GetMapping("addUser") public Result add() { for(int i=0;i<10;i++) { TUser user = new TUser(); //user.setOid(IdWorker.getId()); user.setName("shareing_"+i); user.setAge(i); userService.addUser(user); } return Result.success(); }
2.6 接口统一输出优化
实现方式只需要实现ResponseBodyAdvice接口,重写beforeBodyWrite方法接口。
@RestControllerAdvicepublic class ResponseAdvice implements ResponseBodyAdvice
经过优化后,controller
输出可以根据业务的需求定义输出对象。
@GetMapping("getUserByName") public TUser getUserByName1(@RequestParam String name) { logger.info("getUserByName paramter name:"+name); return userService.getUserByName(name); }
2.7 子系统如何实现
子系统引入common的jar包,
com.xx xx-common 2.0
3、子系统定义状态码,实现BaseResultCode接口
public enum OrderModelErrorCode implements BaseResultCode{ ORDER_STATUS_ERROR(1000, "订单状态不正确"); private int code; private String msg; private UserModelErrorCode(int code, String msg) { this.code = code; this.msg = msg; } @Override public int getCode() { return code; } @Override public String getMsg() { return msg; }}
定义异常处理类,继承公共异常处理类SysExceptionHandler
@RestControllerAdvicepublic class OrderModalExceptionHandle extends SysExceptionHandler{ @Override public Result handleException(HttpServletRequest request, Exception ex) { return super.handleException(request, ex); //子系统可以扩展异常处理 }}
子系统使用示例:
@Overridepublic Order getOrder(String orderId){ Order order =getOrder(orderId); //相关伪代码 if(order.getStatus()>120) { throw new SysException(OrderModelErrorCode.ORDER_STATUS_ERROR); } return order;}
感谢各位的阅读,以上就是"Spring Boot统一接口返回及全局异常处理的方法"的内容了,经过本文的学习后,相信大家对Spring Boot统一接口返回及全局异常处理的方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
接口
统一
处理
方法
输出
全局
子系统
状态
错误
参数
系统
学习
内容
格式
模块
类型
结果
问题
成功
业务
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
描述数据库全体数据的模式
湖南省学it软件开发培训机构
网络安全大赛最新消息
超市标签数据库
服务器有没有无线网
河南高新网络技术有限公司
清理oracle数据库表的数据
网络安全法第四十四条
arm服务器干什么用的
ad域控服务器硬件
护苗网络安全课 通知
对于中文法律数据库的期待
遵守网络安全
江西省网络安全等级测评
stata 部分 数据库
软件开发中的NCI过程
数字网络技术学数学吗
湖北推广软件开发中心
易乐游双服务器
gpg 从公钥服务器接收失败
数据库原理职工编号
软件开发冲突
网络安全伴我行征文800字
显示器连接服务器是什么样的
上海银行软件开发招聘
桌面软件开发费用
网络安全法中规定出售或向
EV2数据库的主页面包括
软件开发中的NCI过程
数据库技术与应用第三版实验