我们目前已经设计出了,包含全局响应,异常错误响应进行了统一返回。但是错误内容我们设计的比较模糊统一,还可以进行细化这样更有利于定位错误
当我们需要调用Http接口时,无论是在Web端还是移动端,都有可能遇到各种错误,例如参数缺失、类型错误、系统错误等。为了规范错误信息的返回,我们需要定义一个统一的接口错误返回值。通过对接口错误返回值的统一设计,我们可以规范调用方对各种不同错误的处理方式,并提供更加详细、准确的错误提示,同时也帮助后端实现接口返回值的规范化设计。因此,接口错误返回值的设计不仅仅是对错误信息的规范化处理,还涉及到业务上的错误设计。这样的设计可以有效地提高接口的使用效率和可维护性。
错误码定义
根据 http stats 错误通常可以分为以下几大类
- 200:请求成功
- 400:请求参数错误
- 401:未授权访问
- 403:表示禁止访问资源。
- 404:表示未找到资源。
- 500:表示服务器内部错误。
错误码的设计,可以借用http错误码+三位api自定义错误码 一共是6位数字,具体每个模块代表什么可以根据你自己的业务逻辑,定义不同数字,位数对应不同模块
对应错误格式如下

错误接口
1package cn.soboys.springbootrestfulapi.common.error; 2 3/** 4 * @author 公众号 程序员三时 5 * @version 1.0 6 * @date 2023/5/2 21:33 7 * @webSite https://github.com/coder-amiao 8 * 错误码接口,凡各模块错误码枚举类,皆须为此接口的子类型 9 */ 10public interface ErrorCode { 11 Integer getCode(); 12 13 String getMessage(); 14 15 boolean getSuccess(); 16}
自定义错误实现枚举
1package cn.soboys.springbootrestfulapi.common.error; 2 3/** 4 * @author 公众号 程序员三时 5 * @version 1.0 6 * @date 2023/5/2 21:36 7 * @webSite https://github.com/coder-amiao 8 */ 9public enum CommonErrorCode implements ErrorCode { 10 11 NOT_FOUND(false, 404, "接口不存在"), 12 FORBIDDEN(false, 403, "资源拒绝访问"), 13 UNAUTHORIZED(false, 401, "未认证(签名错误)"), 14 INTERNAL_SERVER_ERROR(false, 500, "服务网络不可用"), 15 PARAM_ERROR(false, 110001, "参数错误"); 16 17 18 19 CommonErrorCode(Boolean success, Integer code, String message) { 20 this.success = success; 21 this.code = code; 22 this.message = message; 23 24 } 25 26 /** 27 * 响应是否成功 28 */ 29 private Boolean success; 30 /** 31 * 响应状态码 32 */ 33 private Integer code; 34 /** 35 * 响应信息 36 */ 37 private String message; 38 39 40 @Override 41 public Integer getCode() { 42 return code; 43 } 44 45 @Override 46 public String getMessage() { 47 return message; 48 } 49 50 @Override 51 public boolean getSuccess() { 52 return success; 53 } 54 55 56} 57
全局异常错误处理
1package cn.soboys.springbootrestfulapi.common.exception; 2 3 4import cn.hutool.core.collection.CollectionUtil; 5import cn.soboys.springbootrestfulapi.common.error.CommonErrorCode; 6import cn.soboys.springbootrestfulapi.common.resp.R; 7import org.springframework.context.support.DefaultMessageSourceResolvable; 8import org.springframework.validation.BindException; 9import org.springframework.validation.BindingResult; 10import org.springframework.validation.FieldError; 11import org.springframework.web.bind.MethodArgumentNotValidException; 12import org.springframework.web.bind.annotation.ExceptionHandler; 13import org.springframework.web.bind.annotation.ResponseBody; 14import org.springframework.web.bind.annotation.RestControllerAdvice; 15import org.springframework.web.context.request.WebRequest; 16 17import javax.validation.ConstraintViolation; 18import javax.validation.ConstraintViolationException; 19import java.util.List; 20import java.util.stream.Collectors; 21 22 23/** 24 * @author 公众号 程序员三时 25 * @version 1.0 26 * @date 2023/4/29 00:21 27 * @webSite https://github.com/coder-amiao 28 * 统一异常处理器 29 */ 30@RestControllerAdvice 31public class GlobalExceptionHandler { 32 33 /** 34 * 通用异常处理方法 35 **/ 36 @ExceptionHandler(Exception.class) 37 @ResponseBody 38 public R error(Exception e, WebRequest request) { 39 e.printStackTrace(); 40 return R.setResult(CommonErrorCode.INTERNAL_SERVER_ERROR) 41 .request(request.getDescription(true)) 42 .errorMsg(e.getMessage()); 43 } 44 45 46 47 /** 48 * 处理 form data方式调用接口对象参数校验失败抛出的异常 49 */ 50 @ExceptionHandler(BindException.class) 51 @ResponseBody 52 public R BindExceptionHandler(BindException e) { 53 String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining()); 54 return R.failure().code(CommonErrorCode.PARAM_ERROR.getCode()).message(message); 55 } 56 57 /** 58 * 处理Get请求中 验证路径中 单个参数请求失败抛出异常 59 * @param e 60 * @return 61 */ 62 @ExceptionHandler(ConstraintViolationException.class) 63 public R ConstraintViolationExceptionHandler(ConstraintViolationException e) { 64 String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining()); 65 return R.failure().code(CommonErrorCode.PARAM_ERROR.getCode()).message(message); 66 } 67 68 69 /** 70 * 处理 json 请求体调用接口对象参数校验失败抛出的异常 71 */ 72 @ExceptionHandler(MethodArgumentNotValidException.class) 73 public R jsonParamsException(MethodArgumentNotValidException e) { 74 BindingResult bindingResult = e.getBindingResult(); 75 String msg=";"; 76 77 for (FieldError fieldError : bindingResult.getFieldErrors()) { 78 msg = String.format("%s%s;", fieldError.getField(), fieldError.getDefaultMessage())+msg; 79 } 80 return R.failure().code(CommonErrorCode.PARAM_ERROR.getCode()).message(msg); 81 } 82 83 /** 84 * 自定义异常处理方法 85 * 86 * @param e 87 * @return 88 */ 89 @ExceptionHandler(BusinessException.class) 90 @ResponseBody 91 public R error(BusinessException e) { 92 e.printStackTrace(); 93 return R.failure().message(e.getMessage()).code(e.getCode()); 94 } 95 96} 97
具体项目代码已经 同步到github 上,后续会完善更新一系列整合脚手架工具,能够开箱即用

