温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java封装统一的Result Model案例

发布时间:2020-10-08 14:22:20 来源:脚本之家 阅读:204 作者:试水流连 栏目:开发技术

在开发过程中,有时候会需要使用错误码+错误信息的形式,来返回某些业务操作的错误结果信息,来代替效率较低的异常传递。

这样就需要封装一个统一的Result model作为返回值,代替直接返回数据等结果。

1.定义错误码 - 错误信息接口

错误码可能是String、Integer、Long 等类型,也可能是enum类型。因此这里使用泛型来代替,错误码类型,可以提高灵活性

。也可以通过枚举直接实现CodeMessage接口。

public interface CodeMessage<C>{
  C getCode();
  String getMessage();
}

2.实现Result Model

这里定义的是service层的Result。有时候在controller层只会作一些简单的参数校验,在service层会作进一步的校验,

如果controller中需要统一返回一个JsonResult可以将ServiceResult作一个简单的转换器进行转换。

public class ServiceResult<T, C> implements Serializable {

  public static final CodeMessage<String> SUCCESS = new DefaultMessage<>("00000000", "success");

  private T data;
  private CodeMessage<C> message;
  private boolean isSuccess;

  ServiceResult(T data, boolean isSuccess, CodeMessage<C> message) {
    this.data = data;
    this.message = message;
    this.isSuccess = isSuccess;
  }

  public T getData() {
    return data;
  }

  public CodeMessage<C> getCodeMessage() {
    return message;
  }

  public boolean isSuccess() {
    return isSuccess;
  }

  public static <D, C> ServiceResultBuilder<D, C> success() {
    ServiceResultBuilder<D, C> builder = builder();
    return builder.isSuccess(true);
  }

  public static <D, C> ServiceResultBuilder<D, C> success(CodeMessage<C> codeMessage) {
    ServiceResultBuilder<D, C> builder = builder();
    return builder.isSuccess(true).code(codeMessage.getCode()).message(codeMessage.getMessage());
  }

  public static <D> ServiceResult<D, String> success(D data) {
    ServiceResultBuilder<D, String> success = success(SUCCESS);
    return success.data(data).build();
  }

  public static <D, C> ServiceResult<D, C> error(CodeMessage<C> codeMessage) {
    ServiceResultBuilder<D, C> builder = builder();
    return builder.isSuccess(false).code(codeMessage.getCode()).message(codeMessage.getMessage()).build();
  }

  public static <D, C> ServiceResultBuilder<D, C> error() {
    ServiceResultBuilder<D, C> builder = builder();
    return builder.isSuccess(false);
  }


  static <D, C> ServiceResultBuilder<D, C> builder() {
    return new ServiceResultBuilder<>();
  }

  public static class DefaultMessage<C> implements CodeMessage<C>, java.io.Serializable {

    private C code;
    private String message;

    public DefaultMessage(C code, String message) {
      this.code = code;
      this.message = message;
    }

    @Override
    public C getCode() {
      return code;
    }

    @Override
    public String getMessage() {
      return message;
    }

    @Override
    public String toString() {
      return "DefaultMessage{" +
          "code=" + code +
          ", message='" + message + '\'' +
          '}';
    }
  }

  public static class ServiceResultBuilder<T, C> {

    private T data;
    private C code;
    private String message;
    private boolean isSuccess;

    ServiceResultBuilder() { //package private
    }

    public ServiceResultBuilder<T, C> data(T data) {
      this.data = data;
      return this;
    }

    ServiceResultBuilder<T, C> isSuccess(boolean isSuccess) {
      this.isSuccess = isSuccess;
      return this;
    }

    public ServiceResultBuilder<T, C> code(C code) {
      this.code = code;
      return this;
    }

    public ServiceResultBuilder<T, C> message(String message) {
      this.message = message;
      return this;
    }

    public ServiceResult<T, C> build() {
      Objects.requireNonNull(code, "code");
      Objects.requireNonNull(code, "message");
      return new ServiceResult<>(data, isSuccess, new DefaultMessage<>(code, message));
    }
  }

  @Override
  public String toString() {
    return "ServiceResult{" +
        "data=" + data +
        ", message=" + message +
        ", isSuccess=" + isSuccess +
        '}';
  }
}

3.使用

定义枚举错误码

pubilc enum CodeMessageEnum implements CodeMessage<Integer> {
  some_error(1001, "错误信息。");
  Integer code;
  String message;

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

  @Override
  public Integer getCode() {
    return code;
  }

  @Override
  public String getMessage() {
    return message;
  }
}
//返回正确结果带返回值。

ServiceResult<List<String>, String> s = ServiceResult.success(Arrays.asList("1", "2", "3"));
s.isSuccess(); //true
ServiceResult<?, Integer> e1 = ServiceResult.error(CodeMessageEnum.some_error);
e1.isSuccess(); //false
ServiceResult<?, Long> e2 = ServiceResult.error(new ServiceResult.DefaultMessage<>(1000L, "error"));
e2.isSuccess(); //false
//ServiceResult{data=[1, 2, 3], message=DefaultMessage{code=00000000, message='success'}, isSuccess=true}
System.out.println(s);
//ServiceResult{data=null, message=DefaultMessage{code=1001, message='错误信息。'}, isSuccess=false}
System.out.println(e1);
//ServiceResult{data=null, message=DefaultMessage{code=1000, message='error'}, isSuccess=false}
System.out.println(e2);

补充知识:java 封装返回结果实体类 返回结果以及错误信息

我就废话不多说了,大家还是直接看代码吧~

public class ResponseMessage {
  private final static String STATUS_OK = "0";
  private final static String STATUS_ERROR = "1";

  private String status;   // 状态
  private Object data;  // 返回值
  private Exception e;  // 异常类捕获
  private String msg;  // 自定义信息

  public String getStatus() {
    return status;
  }
  public void setStatus(String status) {
    this.status = status;
  }
  public Object getData() {
    return data;
  }
  public void setData(Object data) {
    this.data = data;
  }
  public Exception getE() {
    return e;
  }
  public void setE(Exception e) {
    this.e = e;
  }
  public String getMsg() {
    return msg;
  }
  public void setMsg(String msg) {
    this.msg = msg;
  }
  public ResponseMessage() {
    super();
  }
  public ResponseMessage(String status, Object data, Exception e, String msg) {
    super();
    this.status = status;
    this.data = data;
    this.e = e;
    this.msg = msg;
  }

  /**
   * 成功的结果
   * @param data 返回结果
   * @param msg 返回信息
   */
  public static ResponseMessage newOkInstance(Object data, String msg) {
    return new ResponseMessage(ResponseMessage.STATUS_OK, data, null, msg);
  }

  /**
   * 成功的结果
   * @param data  返回结果
   */
  public ResponseMessage newOkInstance(Object data) {
    return new ResponseMessage(ResponseMessage.STATUS_OK, data, null, null);
  }

  /**
   * 失败的结果
   * @param msg 返回信息
   */
  public static ResponseMessage newErrorInstance(String msg) {
    return new ResponseMessage(ResponseMessage.STATUS_ERROR, null, null, msg);
  }

  /**
   * 失败的结果
   * @param e    异常对象
   * @param msg 返回信息
   */
  public static ResponseMessage newErrorInstance(Exception e, String msg) {
    return new ResponseMessage(ResponseMessage.STATUS_ERROR, null, e, msg);
  }
}

以上这篇Java封装统一的Result Model案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI