package org.hsweb.web.core.message; import com.alibaba.fastjson.JSON; import org.hsweb.commons.DateTimeUtils; import java.io.Serializable; import java.lang.reflect.Field; import java.util.*; /** * 响应消息。controller中处理后,返回此对象,响应请求结果给客户端。 */ public class ResponseMessage implements Serializable { private static final long serialVersionUID = 8992436576262574064L; /** * 是否成功 */ private boolean success; /** * 反馈数据 */ private Object data; /** * 反馈信息 */ private String message; /** * 响应码 */ private int code; /** * 过滤字段:指定需要序列化的字段 */ private transient Map<Class<?>, Set<String>> includes; /** * 过滤字段:指定不需要序列化的字段 */ private transient Map<Class<?>, Set<String>> excludes; private transient boolean onlyData; private transient String callback; public Map<String, Object> toMap() { Map<String, Object> map = new HashMap<>(); map.put("success", this.success); if (data != null) map.put("data", this.getData()); if (message != null) map.put("message", this.getMessage()); map.put("code", this.getCode()); return map; } protected ResponseMessage(String message) { this.code = 500; this.message = message; this.success = false; } protected ResponseMessage(boolean success, Object data) { this.code = success ? 200 : 500; this.data = data; this.success = success; } protected ResponseMessage(boolean success, Object data, int code) { this(success, data); this.code = code; } public ResponseMessage include(Class<?> type, String... fields) { return include(type, Arrays.asList(fields)); } public ResponseMessage include(Class<?> type, Collection<String> fields) { if (includes == null) includes = new HashMap<>(); if (fields == null || fields.isEmpty()) return this; fields.forEach(field -> { if (field.contains(".")) { String tmp[] = field.split("[.]", 2); try { Field field1 = type.getDeclaredField(tmp[0]); if (field1 != null) { include(field1.getType(), tmp[1]); } } catch (Throwable e) { } } else { getStringListFormMap(includes, type).add(field); } }); return this; } public ResponseMessage exclude(Class type, Collection<String> fields) { if (excludes == null) excludes = new HashMap<>(); if (fields == null || fields.isEmpty()) return this; fields.forEach(field -> { if (field.contains(".")) { String tmp[] = field.split("[.]", 2); try { Field field1 = type.getDeclaredField(tmp[0]); if (field1 != null) { exclude(field1.getType(), tmp[1]); } } catch (Throwable e) { } } else { getStringListFormMap(excludes, type).add(field); } }); return this; } public ResponseMessage exclude(Collection<String> fields) { if (excludes == null) excludes = new HashMap<>(); if (fields == null || fields.isEmpty()) return this; Class type; if (data != null) type = data.getClass(); else return this; exclude(type, fields); return this; } public ResponseMessage include(Collection<String> fields) { if (includes == null) includes = new HashMap<>(); if (fields == null || fields.isEmpty()) return this; Class type; if (data != null) type = data.getClass(); else return this; include(type, fields); return this; } public ResponseMessage exclude(Class type, String... fields) { return exclude(type, Arrays.asList(fields)); } public ResponseMessage exclude(String... fields) { return exclude(Arrays.asList(fields)); } public ResponseMessage include(String... fields) { return include(Arrays.asList(fields)); } protected Set<String> getStringListFormMap(Map<Class<?>, Set<String>> map, Class type) { Set<String> list = map.get(type); if (list == null) { list = new HashSet<>(); map.put(type, list); } return list; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public Object getData() { return data; } public ResponseMessage setData(Object data) { this.data = data; return this; } @Override public String toString() { return JSON.toJSONStringWithDateFormat(this, DateTimeUtils.YEAR_MONTH_DAY_HOUR_MINUTE_SECOND); } public int getCode() { return code; } public ResponseMessage setCode(int code) { this.code = code; return this; } public static ResponseMessage fromJson(String json) { return JSON.parseObject(json, ResponseMessage.class); } public Map<Class<?>, Set<String>> getExcludes() { return excludes; } public Map<Class<?>, Set<String>> getIncludes() { return includes; } public ResponseMessage onlyData() { setOnlyData(true); return this; } public void setOnlyData(boolean onlyData) { this.onlyData = onlyData; } public boolean isOnlyData() { return onlyData; } public ResponseMessage callback(String callback) { this.callback = callback; return this; } public String getCallback() { return callback; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public static ResponseMessage ok() { return ok(null); } public static ResponseMessage ok(Object data) { return new ResponseMessage(true, data); } public static ResponseMessage created(Object data) { return new ResponseMessage(true, data, 201); } public static ResponseMessage error(String message) { return new ResponseMessage(message); } public static ResponseMessage error(String message, int code) { return new ResponseMessage(message).setCode(code); } }