ResponseResult.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package com.fd.result;
  2. import com.fasterxml.jackson.annotation.JsonFormat;
  3. import com.fasterxml.jackson.annotation.JsonInclude;
  4. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  5. import org.springframework.context.support.DefaultMessageSourceResolvable;
  6. import org.springframework.validation.BindingResult;
  7. import java.io.Serializable;
  8. import java.util.*;
  9. public class ResponseResult implements Serializable {
  10. private static final long serialVersionUID = 2719931935414658118L;
  11. private final Integer status;
  12. private final String message;
  13. @JsonInclude(value = Include.NON_NULL)
  14. private final Object data;
  15. @JsonInclude(value = Include.NON_EMPTY)
  16. private final String[] exceptions;
  17. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  18. private final Date timestamp;
  19. public ResponseResult(Integer status, String message) {
  20. super();
  21. this.status = status;
  22. this.message = message;
  23. this.data = null;
  24. this.timestamp = addTime();
  25. this.exceptions = null;
  26. }
  27. public ResponseResult(ResultData<?> result) {
  28. super();
  29. this.status = result.getState().getStatus();
  30. this.message = result.getState().getMessage();
  31. this.data = result.getData();
  32. this.timestamp = addTime();
  33. this.exceptions = null;
  34. }
  35. public ResponseResult(ResponseMessage rm){
  36. super();
  37. this.status = rm.getStatus();
  38. this.message = rm.getMessage();
  39. this.data = null;
  40. this.timestamp = addTime();
  41. this.exceptions = null;
  42. }
  43. public ResponseResult(Integer status, Object data) {
  44. super();
  45. this.status = status;
  46. this.message = null;
  47. this.data = data;
  48. this.timestamp = addTime();
  49. this.exceptions = null;
  50. }
  51. public ResponseResult(ResponseMessage responseMessage, BindingResult result) {
  52. super();
  53. List<Map<String, String>> errors = new ArrayList<Map<String, String>>();
  54. for(int i=0;i<result.getAllErrors().size();i++){
  55. Map<String, String> map = new HashMap<String, String>();
  56. DefaultMessageSourceResolvable dm = (DefaultMessageSourceResolvable) result.getAllErrors().get(i).getArguments()[0];
  57. map.put("field", dm.getDefaultMessage());
  58. map.put("message", result.getAllErrors().get(i).getDefaultMessage());
  59. errors.add(map);
  60. }
  61. this.status = responseMessage.getStatus();
  62. this.message = responseMessage.getMessage();
  63. this.data = errors;
  64. this.timestamp = addTime();
  65. this.exceptions = null;
  66. }
  67. public ResponseResult(Integer status, String message, String key, Object value) {
  68. super();
  69. this.status = status;
  70. this.message = message;
  71. Map<String, Object> map = new HashMap<String, Object>();
  72. if (key == null || ("").equals(key)) {
  73. map.put("key", value);
  74. } else {
  75. map.put(key, value);
  76. }
  77. this.data = map;
  78. this.timestamp = addTime();
  79. this.exceptions = null;
  80. }
  81. public ResponseResult(Integer status, Throwable ex) {
  82. super();
  83. this.status = status;
  84. this.message = ex.getMessage();
  85. this.data = null;
  86. StackTraceElement[] stackTeanceElement = ex.getStackTrace();
  87. this.exceptions = new String[stackTeanceElement.length];
  88. for (int i = 0; i < stackTeanceElement.length; i++) {
  89. this.exceptions[i] = stackTeanceElement[i].toString();
  90. }
  91. this.timestamp = addTime();
  92. }
  93. public ResponseResult(Integer status, String message, Throwable ex) {
  94. super();
  95. this.status = status;
  96. this.message = message;
  97. this.data = null;
  98. StackTraceElement[] stackTeanceElement = ex.getStackTrace();
  99. this.exceptions = new String[stackTeanceElement.length];
  100. for (int i = 0; i < stackTeanceElement.length; i++) {
  101. this.exceptions[i] = stackTeanceElement[i].toString();
  102. }
  103. this.timestamp = addTime();
  104. }
  105. public Integer getStatus() {
  106. return status;
  107. }
  108. public String getMessage() {
  109. return message;
  110. }
  111. public Object getData() {
  112. return data;
  113. }
  114. public String[] getExceptions() {
  115. return exceptions;
  116. }
  117. public Date getTimestamp() {
  118. return timestamp;
  119. }
  120. public Date addTime(){
  121. Calendar nowTime= Calendar.getInstance();
  122. nowTime.add(Calendar.MINUTE,15);
  123. Date time = nowTime.getTime();
  124. return time;
  125. }
  126. }