数据流动之使用泛型类向前端传递数据

1.使用Result<T>类来封装发送给前端的数据

  Result<T>类功能:封装需要向前端传递的数据。

  Result<T>类的内部构造:在内部封装三个元素,code,msg,T  data;和两个构造方法,一个用来传递成功信息,另一个用来传递失败信息。

  

 1 package com.app.miaosha.Result;
 2 
 3 import lombok.Data;
 4 
 5 @Data
 6 public class Result<T> {
 7     private int code;
 8     private String msg;
 9     private T data;
10 
11 
12     private Result(T data){
13         this.code=0;
14         this.msg="success";
15         this.data=data;
16     }
17 
18     private Result(CodeMsg msg){
19         if (msg == null) {
20             return;
21         }
22         this.code = msg.getCode();
23         this.msg = msg.getMsg();
24     }
25 
26 
27     public static <T> Result<T> success(T data) {
28         return new Result<T>(data);
29     }
30     public static <T> Result<T> error(CodeMsg codeMsg){
31         return new Result<T>(codeMsg);
32     }
33 
34 }

2.失败信息的封装类CodeMsg

  为方便用户传输失败信息而建立。包括两个参数:code,message。

  

package com.app.miaosha.Result;

import lombok.Data;



@Data
public class CodeMsg {
    private int code;
    private String msg;

    //通用异常
    public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务端异常!");
    public static CodeMsg BIND_ERROR = new CodeMsg(500101, "参数校验异常:%s!");
    public static CodeMsg REQUEST_ILLEGAL = new CodeMsg(500102, "请求非法!");
    public static CodeMsg ACCESS_LIMIT_REACHED = new CodeMsg(500103, "访问太频繁!");



    //登录模块 5002XX
    public static CodeMsg SESSION_ERROR = new CodeMsg(500210, "Session不存在或者已经失效!");
    public static CodeMsg PASSWORD_EMPTY = new CodeMsg(500211, "登录密码不能为空!");
    public static CodeMsg MOBILE_EMPTY = new CodeMsg(500212, "手机号码不能为空!");
    public static CodeMsg MOBILE_ERROR = new CodeMsg(500213, "手机号码格式错误!");
    public static CodeMsg MOBILE_NOT_EXIST = new CodeMsg(500214, "用户不存在!");
    public static CodeMsg PASSWORD_ERROR = new CodeMsg(500215, "密码错误!");
    public static CodeMsg USER_NO_LOGIN = new CodeMsg(500216, "用户未登录!");



    //订单模块 5004XX
    public static CodeMsg ORDER_NOT_EXIST = new CodeMsg(500400, "订单不存在!");



    //秒杀模块 5005XX
    public static CodeMsg SECKILL_OVER = new CodeMsg(500500, "商品已经秒杀完毕!");
    public static CodeMsg REPEATE_SECKILL = new CodeMsg(500501, "不能重复秒杀!");
    public static CodeMsg SECKILL_FAIL = new CodeMsg(500502, "秒杀失败!");



    private CodeMsg(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    @Override
    public String toString() {
        return "CodeMsg{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                '}';
    }
}
View Code

 

 1   @Data
 2   public class CodeMsg {
 3     private int code;
 4     private String msg;
 5 
 6     //通用异常
 7     public static CodeMsg SUCCESS = new CodeMsg(0, "成功!");
 8     public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务端异常!");
 9     public static CodeMsg BIND_ERROR = new CodeMsg(500101, "参数校验异常:%s!");
10     public static CodeMsg REQUEST_ILLEGAL = new CodeMsg(500102, "请求非法!");
11     public static CodeMsg ACCESS_LIMIT_REACHED = new CodeMsg(500103, "访问太频繁!");
12 
13 
14 
15     //登录模块 5002XX
16     public static CodeMsg SESSION_ERROR = new CodeMsg(500210, "Session不存在或者已经失效!");
17     public static CodeMsg PASSWORD_EMPTY = new CodeMsg(500211, "登录密码不能为空!");
18     public static CodeMsg MOBILE_EMPTY = new CodeMsg(500212, "手机号码不能为空!");
19     public static CodeMsg MOBILE_ERROR = new CodeMsg(500213, "手机号码格式错误!");
20     public static CodeMsg MOBILE_NOT_EXIST = new CodeMsg(500214, "用户不存在!");
21     public static CodeMsg PASSWORD_ERROR = new CodeMsg(500215, "密码错误!");
22     public static CodeMsg USER_NO_LOGIN = new CodeMsg(500216, "用户未登录!");
23 
24 
25 
26     //订单模块 5004XX
27     public static CodeMsg ORDER_NOT_EXIST = new CodeMsg(500400, "订单不存在!");
28 
29 
30 
31     //秒杀模块 5005XX
32     public static CodeMsg SECKILL_OVER = new CodeMsg(500500, "商品已经秒杀完毕!");
33     public static CodeMsg REPEATE_SECKILL = new CodeMsg(500501, "不能重复秒杀!");
34     public static CodeMsg SECKILL_FAIL = new CodeMsg(500502, "秒杀失败!");
35 
36 
37 
38     private CodeMsg(int code, String msg) {
39         this.code = code;
40         this.msg = msg;
41     }
42 
43 
44     @Override
45     public String toString() {
46         return "CodeMsg{" +
47                 "code=" + code +
48                 ", msg='" + msg + '\'' +
49                 '}';
50     }
51 }

 

 

  

posted on 2020-04-08 20:37  hello,bdiskl  阅读(542)  评论(0编辑  收藏  举报

导航