由于现在大部分公司项目都是前后端分离架构,为了联调方便统一后端数据返回格式,统一结果封装
统一返回结果实体类
/**
* 统一结果封装
*/
@Data
public class R {
private Integer code;
private String message;
private Map<String, Object> data = new HashMap<>();
/**
* 构造器私有
*/
private R(){}
/**
* 返回成功
*/
public static R ok(){
R r = new R();
r.setCode(ResponseEnum.SUCCESS.getCode());
r.setMessage(ResponseEnum.SUCCESS.getMessage());
return r;
}
/**
* 返回失败
*/
public static R error(){
R r = new R();
r.setCode(ResponseEnum.ERROR.getCode());
r.setMessage(ResponseEnum.ERROR.getMessage());
return r;
}
/**
* 设置特定结果
*/
public static R setResult(ResponseEnum responseEnum){
R r = new R();
r.setCode(responseEnum.getCode());
r.setMessage(responseEnum.getMessage());
return r;
}
/**
* 设置特定信息
*/
public R message(String message){
this.setMessage(message);
return this;
}
/**
* 设置特定状态码
*/
public R code(Integer code){
this.setCode(code);
return this;
}
public R data(String key, Object value){
this.data.put(key, value);
return this;
}
public R data(Map<String, Object> map){
this.setData(map);
return this;
}
}
返回结果响应枚举类,可根据不同业务自行增删
@Getter
@AllArgsConstructor
@ToString
public enum ResponseEnum {
SUCCESS(0, "成功"),
ERROR(-1, "服务器内部错误"),
//-1xx 服务器错误
BAD_SQL_GRAMMAR_ERROR(-101, "sql语法错误"),
SERVLET_ERROR(-102, "servlet请求异常"),
UPLOAD_ERROR(-103, "文件上传错误"),
EXPORT_DATA_ERROR(104, "数据导出失败"),
//-2xx 参数校验
RECHARGE_AMOUNT_NULL_ERROR(-201, "充值额度不能为空"),
MOBILE_NULL_ERROR(-202, "手机号码不能为空"),
MOBILE_ERROR(-203, "手机号码不正确"),
PASSWORD_NULL_ERROR(204, "密码不能为空"),
CODE_NULL_ERROR(205, "验证码不能为空"),
CODE_ERROR(206, "验证码错误"),
MOBILE_EXIST_ERROR(207, "手机号已被注册"),
LOGIN_USER_ERROR(208, "用户不存在"),
LOGIN_PASSWORD_ERROR(209, "密码错误"),
LOGIN_LOCKED_ERROR(210, "用户被锁定"),
LOGIN_AUTH_ERROR(-211, "未登录或登录已过期,请登录"),
LOGIN_DISABLED_ERROR(-212, "用户已被禁用"),
CODE_EXPIRE_ERROR(-213, "验证码已过期,请重新获取"),
ADDRESS_NULL_ERROR(-214, "收货地址不能为空"),
RECEIVER_NULL_ERROR(-214, "收货人不能为空"),
NOT_SUFFICIENT_FUNDS_ERROR(301, "余额不足,请充值"),
NAME_NULL_ERROR(302, "名称不能为空"),
PAY_UNIFIEDORDER_ERROR(401, "统一下单错误"),
ALIYUN_RESPONSE_FAIL(-501, "阿里云短信服务响应失败"),
ALIYUN_SMS_LIMIT_CONTROL_ERROR(-502, "短信发送过于频繁"),//业务限流,
ALIYUN_SMS_ERROR(-503, "短信发送失败"),//其他失败
WEIXIN_CALLBACK_PARAM_ERROR(-601, "回调参数不正确"),
WEIXIN_FETCH_ACCESSTOKEN_ERROR(-602, "获取access_token失败"),
WEIXIN_FETCH_USERINFO_ERROR(-603, "获取用户信息失败"),
;
// 响应状态码
private final Integer code;
// 响应信息
private final String message;
}
为了预防由于项目代码问题导致的异常情况出现,统一格式化后端异常错误响应数据
如下项目代码问题:500,哈哈哈
注解@RestControllerAdvice
项目中自定义异常:
/**
* 业务异常处理类
*/
@Data
@NoArgsConstructor
public class BusinessException extends RuntimeException {
//状态码
private Integer code;
//错误消息
private String message;
/**
* @param message 错误信息
*/
public BusinessException(String message) {
this.code = ResponseEnum.ERROR.getCode();
this.message = message;
}
/**
* @param message 错误信息
* @param code 错误码
*/
public BusinessException(String message, Integer code) {
this.code = code;
this.message = message;
}
/**
* @param responseEnum 响应枚举
*/
public BusinessException(ResponseEnum responseEnum) {
code = responseEnum.getCode();
message = responseEnum.getMessage();
}
/**
* @param responseEnum 响应枚举
* @param throwable 原始异常对象
*/
public BusinessException(ResponseEnum responseEnum, Throwable throwable) {
super(throwable);
code = responseEnum.getCode();
message = responseEnum.getMessage();
}
}
统一异常处理类,拦截不同类型的异常,进行统一格式数据返回
/**
* 统一异常处理
*/
@Slf4j
@RestControllerAdvice //在controller层添加通知。如果使用@ControllerAdvice,则方法上需要添加@ResponseBody
public class UnifiedExceptionHandler {
/**
* 未定义异常
*/
@ExceptionHandler(value = Exception.class) //当controller中抛出Exception,则捕获
public R handleException(Exception e) {
log.error(e.getMessage(), e);
return R.error();
}
/**
* 自定义异常
*/
@ExceptionHandler(value = BusinessException.class)
public R handleBusinessException(BusinessException e) {
log.error(e.getMessage(), e);
return R.error().message(e.getMessage()).code(e.getCode());
}
/**
* Controller上一层相关异常
*/
@ExceptionHandler({
NoHandlerFoundException.class,
HttpRequestMethodNotSupportedException.class,
HttpMediaTypeNotSupportedException.class,
MissingPathVariableException.class,
MissingServletRequestParameterException.class,
TypeMismatchException.class,
HttpMessageNotReadableException.class,
HttpMessageNotWritableException.class,
MethodArgumentNotValidException.class,
HttpMediaTypeNotAcceptableException.class,
ServletRequestBindingException.class,
ConversionNotSupportedException.class,
MissingServletRequestPartException.class,
AsyncRequestTimeoutException.class
})
public R handleServletException(Exception e) {
log.error(e.getMessage(), e);
//SERVLET_ERROR(-102, "servlet请求异常"),
return R.error().message(ResponseEnum.SERVLET_ERROR.getMessage()).code(ResponseEnum.SERVLET_ERROR.getCode());
}
}
可以使用断言的方式更加优雅的判断异常,断言类
@Slf4j
public abstract class Assert {
/**
* 断言对象不为空
* 如果对象obj为空,则抛出异常
* @param obj 待判断对象
*/
public static void notNull(Object obj, ResponseEnum responseEnum) {
if (obj == null) {
log.info("obj is null...............");
throw new BusinessException(responseEnum);
}
}
/**
* 断言对象为空
* 如果对象obj不为空,则抛出异常
* @param object 判断对象
* @param responseEnum 响应枚举类型
*/
public static void isNull(Object object, ResponseEnum responseEnum) {
if (object != null) {
log.info("obj is not null......");
throw new BusinessException(responseEnum);
}
}
/**
* 断言表达式为真
* 如果不为真,则抛出异常
*
* @param expression 是否成功
*/
public static void isTrue(boolean expression, ResponseEnum responseEnum) {
if (!expression) {
log.info("fail...............");
throw new BusinessException(responseEnum);
}
}
/**
* 断言两个对象不相等
* 如果相等,则抛出异常
* @param m1 一个对象
* @param m2 另一个对象
* @param responseEnum 响应枚举类型
*/
public static void notEquals(Object m1, Object m2, ResponseEnum responseEnum) {
if (m1.equals(m2)) {
log.info("equals...............");
throw new BusinessException(responseEnum);
}
}
/**
* 断言两个对象相等
* 如果不相等,则抛出异常
* @param m1 一个对象
* @param m2 另一个对象
* @param responseEnum 响应枚举类型
*/
public static void equals(Object m1, Object m2, ResponseEnum responseEnum) {
if (!m1.equals(m2)) {
log.info("not equals...............");
throw new BusinessException(responseEnum);
}
}
/**
* 断言参数不为空
* 如果为空,则抛出异常
* @param s 字符串
* @param responseEnum 响应枚举类型
*/
public static void notEmpty(String s, ResponseEnum responseEnum) {
if (StringUtils.isEmpty(s)) {
log.info("is empty...............");
throw new BusinessException(responseEnum);
}
}
}
文件目录: