码迷,mamicode.com
首页 > 编程语言 > 详细

Java秒杀系统---1.使用Result<>向前端传递数据

时间:2020-04-08 20:44:20      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:display   tostring   access   ror   public   sed   传递   src   gif   

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

 

 

  

Java秒杀系统---1.使用Result<>向前端传递数据

标签:display   tostring   access   ror   public   sed   传递   src   gif   

原文地址:https://www.cnblogs.com/deijiawoyu/p/12662599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!