123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- package com.fd.result;
- import com.fasterxml.jackson.annotation.JsonFormat;
- import com.fasterxml.jackson.annotation.JsonInclude;
- import com.fasterxml.jackson.annotation.JsonInclude.Include;
- import org.springframework.context.support.DefaultMessageSourceResolvable;
- import org.springframework.validation.BindingResult;
- import java.io.Serializable;
- import java.util.*;
- public class ResponseResult implements Serializable {
- private static final long serialVersionUID = 2719931935414658118L;
- private final Integer status;
- private final String message;
- @JsonInclude(value = Include.NON_NULL)
- private final Object data;
- @JsonInclude(value = Include.NON_EMPTY)
- private final String[] exceptions;
- @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
- private final Date timestamp;
- public ResponseResult(Integer status, String message) {
- super();
- this.status = status;
- this.message = message;
- this.data = null;
- this.timestamp = addTime();
- this.exceptions = null;
- }
- public ResponseResult(ResultData<?> result) {
- super();
- this.status = result.getState().getStatus();
- this.message = result.getState().getMessage();
- this.data = result.getData();
- this.timestamp = addTime();
- this.exceptions = null;
- }
- public ResponseResult(ResponseMessage rm){
- super();
-
- this.status = rm.getStatus();
- this.message = rm.getMessage();
- this.data = null;
- this.timestamp = addTime();
- this.exceptions = null;
- }
-
- public ResponseResult(Integer status, Object data) {
- super();
- this.status = status;
- this.message = null;
- this.data = data;
- this.timestamp = addTime();
- this.exceptions = null;
- }
-
- public ResponseResult(ResponseMessage responseMessage, BindingResult result) {
- super();
-
- List<Map<String, String>> errors = new ArrayList<Map<String, String>>();
-
- for(int i=0;i<result.getAllErrors().size();i++){
- Map<String, String> map = new HashMap<String, String>();
- DefaultMessageSourceResolvable dm = (DefaultMessageSourceResolvable) result.getAllErrors().get(i).getArguments()[0];
- map.put("field", dm.getDefaultMessage());
- map.put("message", result.getAllErrors().get(i).getDefaultMessage());
- errors.add(map);
- }
-
- this.status = responseMessage.getStatus();
- this.message = responseMessage.getMessage();
- this.data = errors;
- this.timestamp = addTime();
- this.exceptions = null;
- }
- public ResponseResult(Integer status, String message, String key, Object value) {
- super();
- this.status = status;
- this.message = message;
- Map<String, Object> map = new HashMap<String, Object>();
- if (key == null || ("").equals(key)) {
- map.put("key", value);
- } else {
- map.put(key, value);
- }
- this.data = map;
- this.timestamp = addTime();
- this.exceptions = null;
- }
- public ResponseResult(Integer status, Throwable ex) {
- super();
- this.status = status;
- this.message = ex.getMessage();
- this.data = null;
- StackTraceElement[] stackTeanceElement = ex.getStackTrace();
- this.exceptions = new String[stackTeanceElement.length];
- for (int i = 0; i < stackTeanceElement.length; i++) {
- this.exceptions[i] = stackTeanceElement[i].toString();
- }
- this.timestamp = addTime();
- }
- public ResponseResult(Integer status, String message, Throwable ex) {
- super();
- this.status = status;
- this.message = message;
- this.data = null;
- StackTraceElement[] stackTeanceElement = ex.getStackTrace();
- this.exceptions = new String[stackTeanceElement.length];
- for (int i = 0; i < stackTeanceElement.length; i++) {
- this.exceptions[i] = stackTeanceElement[i].toString();
- }
- this.timestamp = addTime();
- }
- public Integer getStatus() {
- return status;
- }
- public String getMessage() {
- return message;
- }
- public Object getData() {
- return data;
- }
- public String[] getExceptions() {
- return exceptions;
- }
- public Date getTimestamp() {
- return timestamp;
- }
- public Date addTime(){
- Calendar nowTime= Calendar.getInstance();
- nowTime.add(Calendar.MINUTE,15);
- Date time = nowTime.getTime();
- return time;
- }
- }
|