CodeMsg类


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

Result类


  1. public class Result<T> {
  2. private int code;
  3. private String msg;
  4. private T data;
  5. /**
  6. * 成功时候的调用
  7. * */
  8. public static <T> Result<T> success(T data){
  9. return new Result<T>(data);
  10. }
  11. /**
  12. * 失败时候的调用
  13. * */
  14. public static <T> Result<T> error(CodeMsg codeMsg){
  15. return new Result<T>(codeMsg);
  16. }
  17. private Result(T data) {
  18. this.data = data;
  19. }
  20. private Result(int code, String msg) {
  21. this.code = code;
  22. this.msg = msg;
  23. }
  24. private Result(CodeMsg codeMsg) {
  25. if(codeMsg != null) {
  26. this.code = codeMsg.getCode();
  27. this.msg = codeMsg.getMsg();
  28. }
  29. }
  30. public int getCode() {
  31. return code;
  32. }
  33. public void setCode(int code) {
  34. this.code = code;
  35. }
  36. public String getMsg() {
  37. return msg;
  38. }
  39. public void setMsg(String msg) {
  40. this.msg = msg;
  41. }
  42. public T getData() {
  43. return data;
  44. }
  45. public void setData(T data) {
  46. this.data = data;
  47. }
  48. }