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> errors = new ArrayList>(); for(int i=0;i map = new HashMap(); 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 map = new HashMap(); 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; } }