从项目中头一个工具类R,用于返回结果的封装,挺好用的

267 阅读1分钟
package com.wxh.Utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author wxh
 * @data 2021/9/11
 */
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "响应信息主体")
public class R<T> implements Serializable {

   private static final long serialVersionUID = 1L;

   @Getter
   @Setter
   @ApiModelProperty(value = "返回标记:成功标记=0,失败标记=1")
   private int code;

   @Getter
   @Setter
   @ApiModelProperty(value = "返回信息")
   private String msg;

   @Getter
   @Setter
   @ApiModelProperty(value = "数据")
   private T data;

   public static <T> R<T> ok() {
      return restResult(null, CommonConstants.SUCCESS, null);
   }

   public static <T> R<T> ok(T data) {
      return restResult(data, CommonConstants.SUCCESS, null);
   }

   public static <T> R<T> ok(T data, String msg) {
      return restResult(data, CommonConstants.SUCCESS, msg);
   }

   public static <T> R<T> failed() {
      return restResult(null, CommonConstants.FAIL, null);
   }

   public static <T> R<T> failed(String msg) {
      return restResult(null, CommonConstants.FAIL, msg);
   }

   public static <T> R<T> failed(T data) {
      return restResult(data, CommonConstants.FAIL, null);
   }

   public static <T> R<T> failed(T data, String msg) {
      return restResult(data, CommonConstants.FAIL, msg);
   }

   private static <T> R<T> restResult(T data, int code, String msg) {
      R<T> apiResult = new R<>();
      apiResult.setCode(code);
      apiResult.setData(data);
      apiResult.setMsg(msg);
      return apiResult;
   }
}

CommonConstants,这里用接口定义这些常量好吗?

很好,更加干净,没有方法实现,只能声明,而且声明的级别都是public static final的。

package com.wxh.Utils;

/**
 * @author wxh
 * @data 2021/9/11
 */
public interface CommonConstants {
   /**
    * 成功标记
    */
   Integer SUCCESS = 0;

   /**
    * 失败标记
    */
   Integer FAIL = 1;
}