接口返回结果封装类

一、背景

作为一名java程序猿,我们通常在工作中要开发接口给前端调用,因此一个统一规范的返回结果是必然的,下面我们就统一返回结果和统一异常处理进行个封装。

二、实现

2.1 枚举状态码

首先返回结果我们需要一个统一的状态码及信息提示,这里我们使用枚举类型,当然也可以使用静态变量形式,个人比较推荐枚举,主要就是看着简洁。

package com.example.demo.response;

public enum ResultEnum {
    // 枚举定义
    SUCCESS(200, "success"),
    NO_PERMISSION(403,"无查看权限"),
    INTERNAL_SERVER_ERROR(500, "服务器异常请联系管理员"),
    ;

    /** 错误码 */
    private Integer code;

    /** 错误信息 */
    private String message;

    ResultEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}

2.2 自定义异常类

package com.example.demo.response;

import lombok.Data;

@Data
public class BusinessException extends RuntimeException {
    /**
     * 错误状态码
     */
    protected Integer errorCode;
    /**
     * 错误提示
     */
    protected String errorMsg;

    public BusinessException(){

    }

    public BusinessException(Integer errorCode, String errorMsg) {
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }
}

2.3 统一返回数据类型

我们将返回结果信息规范封装一下。

package com.example.demo.response;

import lombok.Data;

@Data
public class ResultResponse {
   //是否成功
   private Boolean success;
   //状态码
   private Integer code;
   //提示信息
   private String msg;
   //数据
   private Object data;

   public ResultResponse() {}
   // 自定义构造方法
   public ResultResponse(Boolean success,Integer code, String msg,Object data) {
       this.success = success;
       this.code = code;
       this.msg = msg;
       this.data = data;
   }
   // 自定义异常
   public static ResultResponse customException(BusinessException be){
       ResultResponse result = new ResultResponse();
       result.setSuccess(false);
       result.setCode(be.getErrorCode());
       result.setMsg(be.getErrorMsg());
       result.setData(null);
       return result;
   }
   // 其他异常
   public static ResultResponse otherException(ResultEnum resultEnum){
       ResultResponse result = new ResultResponse();
       result.setSuccess(false);
       result.setCode(resultEnum.getCode());
       result.setMsg(resultEnum.getMessage());
       result.setData(null);
       return result;
   }
}

2.4 全局异常处理器

全局异常处理器就是在你需要报错的地方,直接throws出去,写入自定义的异常信息。

package com.example.demo.response;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理自定义异常
     *
     */
    @ExceptionHandler(value = BusinessException.class)
    public ResultResponse customExceptionHandler(BusinessException e) {
        log.error(e.getMessage(), e);
        return ResultResponse.customException(e);
    }

    /**
     *处理其他异常
     *
     */
    @ExceptionHandler(value = Exception.class)
    public ResultResponse otherExceptionHandler(Exception e) {
        log.error(e.getMessage(), e);
        return ResultResponse.otherException(ResultEnum.INTERNAL_SERVER_ERROR);
    }
}

2.5 测试controller

我们编写一个controller类型,写一个方法测试一下自定义异常和返回封装结果体。

package com.example.demo.response;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("exception")
@Controller
public class ExceptionController {

   @RequestMapping("query")
   @ResponseBody
   public Object exceptionTest() {
       throw new BusinessException(ResultEnum.NO_PERMISSION.getCode(), ResultEnum.NO_PERMISSION.getMessage());
   }
}

2.6 测试结果

启动项目后,在浏览器里输入请求链接,可得下结果:
在这里插入图片描述

三、总结

这是工作学习中总结出来的,感觉应该大多数场景下都够用了,欢迎建议指教啊。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值