JsonResult.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package com.gyee.impala.common.result;
  2. import com.alibaba.fastjson.JSONObject;
  3. import lombok.Data;
  4. import java.io.Serializable;
  5. import java.util.HashMap;
  6. @Data
  7. public class JsonResult extends HashMap<String, Object> implements Serializable {
  8. private Integer code;
  9. private String message;
  10. private Object data;
  11. private ResultCode resultCode;
  12. public static JSONObject error(){
  13. JSONObject json = new JSONObject();
  14. json.put("code", ResultCode.SUCCESS.getCode());
  15. json.put("msg", ResultCode.SUCCESS.getMessage());
  16. return json;
  17. }
  18. public static JSONObject error(ResultCode resultCode){
  19. JSONObject json = new JSONObject();
  20. json.put("code", resultCode.getCode());
  21. json.put("msg", resultCode.getMessage());
  22. return json;
  23. }
  24. public static JSONObject error(int code, String message){
  25. JSONObject json = new JSONObject();
  26. json.put("code", code);
  27. json.put("msg", message);
  28. return json;
  29. }
  30. public static JSONObject success(){
  31. JSONObject json = new JSONObject();
  32. json.put("code", ResultCode.SUCCESS.getCode());
  33. json.put("msg", ResultCode.SUCCESS.getMessage());
  34. return json;
  35. }
  36. public static JSONObject seccess(int code, String message){
  37. JSONObject json = new JSONObject();
  38. json.put("code", code);
  39. json.put("msg", message);
  40. return json;
  41. }
  42. public static JSONObject success(ResultCode resultCode){
  43. JSONObject json = new JSONObject();
  44. json.put("code", resultCode.getCode());
  45. json.put("msg", resultCode.getMessage());
  46. return json;
  47. }
  48. public static JSONObject successData(ResultCode code, Object data){
  49. JSONObject json = new JSONObject();
  50. json.put("code", code.getCode());
  51. json.put("msg", code.getMessage());
  52. json.put("data", data != null ? data : null);
  53. return json;
  54. }
  55. }