Spring Cloud之统一结果封装

我们在做前后端分离架构的设计时最好封装统一的返回结果格式,这样不管我们的API有多少个开发人员开发都能保证返回值风格统一,方便前端人员调试和友好提示,我们可以把这样的功用代码打成jar包在每个微服务中导入去使用。接下来我们看看如何封装统一返回结果类。

这里我们封装了ResponseData类,一般来说返回结果类中有三个必要的要素:

返回状态码//如200、404表示

返回消息//异常的原因等

返回数据//

所以可得到封装如下:

public class ResponseData {
   private int code = 200;
   private String message = "";
   private Object data;
   
   public static ResponseData ok(Object data) {
      return new ResponseData(data);
   }
   
   public static ResponseData fail() {
      return new ResponseData(null);
   }
   
   public static ResponseData fail(String message) {
      return new ResponseData(message);
   }
   
   public static ResponseData fail(String message, int code) {
      return new ResponseData(message, code);
   }
   
   public static ResponseData failByParam(String message) {
      return new ResponseData(message, ResponseCode.PARAM_ERROR_CODE.getCode());
   }
   
   public ResponseData(Object data) {
      super();
      this.data = data;
   }
   
   public ResponseData(String message) {
      super();
      this.message = message;
   }
   
   public ResponseData(String message, int code) {
      super();
      this.message = message;
      this.code = code;
   }
   
   public ResponseData() {
      super();
   }
   
   public int getCode() {

      return code;
   }
   public void setCode(int code) {
      this.code = code;
   }
   public String getMessage() {

      return message;
   }
   public void setMessage(String message) {

      this.message = message;
   }
   public Object getData() {
      return data;
   }
   public void setData(Object data) {

      this.data = data;
   }
   
}

我们给了code默认值为200,毕竟异常情况是少数的。为了减少调用封装类时对参数的设置,我们封装了几个常用方法。

public static ResponseData ok(Object data) {
   return new ResponseData(data);
}
public static ResponseData fail() {
   return new ResponseData(null);
}
public static ResponseData fail(String message) {
   return new ResponseData(message);
}
public static ResponseData fail(String message, int code) {
   return new ResponseData(message, code);
}
public static ResponseData failByParam(String message) {
   return new ResponseData(message, ResponseCode.PARAM_ERROR_CODE.getCode());
}

我们看一下我们在controller中怎么使用统一结果封装类 

@PostMapping("/token")
public ResponseData auth(AuthQuery query) throws Exception {
   if (StringUtils.isBlank(query.getAccessKey()) || StringUtils.isBlank(query.getSecretKey())) {
      return ResponseData.failByParam("accessKey and secretKey not null");
   }
   //查询库中用户信息
   User user = authService.auth(query);
   if (user == null) {
      return ResponseData.failByParam("认证失败");
   }
   JWTUtils jwt = JWTUtils.getInstance(rsaConf.getModulus(),rsaConf.getPrivateExponent(),rsaConf.getPublicExponent());
   return ResponseData.ok(jwt.getToken(user.getId().toString()));
}

我们看一下返回值长啥样:

 查看作者原文:http://www.sucai66.com/article/detail/20200708/26.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值