Last active
April 24, 2019 08:47
-
-
Save An0nymous0/c3eb329c10338ff06699ea9643093c9f to your computer and use it in GitHub Desktop.
[JAVA RestFul Response] java返回工具类 #java #util #common
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.demo.servicemesh1.common; | |
import com.alibaba.fastjson.JSON; | |
import java.io.Serializable; | |
import java.lang.reflect.Field; | |
import java.util.*; | |
public class ResponseMessage<T> implements Serializable { | |
private static final long serialVersionUID = 8992436576262574064L; | |
/** | |
* 消息 | |
*/ | |
protected String message; | |
/** | |
* 返回 | |
*/ | |
protected T result; | |
/** | |
* 必须是标准的HTTP状态 | |
*/ | |
protected int status; | |
/** | |
* 调用时间 | |
*/ | |
private Long timestamp; | |
/** | |
* 业务状态 | |
*/ | |
private String code; | |
public String getMessage() { | |
return message; | |
} | |
public int getStatus() { | |
return status; | |
} | |
public T getResult() { | |
return result; | |
} | |
public Long getTimestamp() { | |
return timestamp; | |
} | |
public String getCode() { | |
return code; | |
} | |
public static <T> ResponseMessage<T> error(String message) { | |
return error(500, message); | |
} | |
public static <T> ResponseMessage<T> error(int status, String message) { | |
ResponseMessage<T> msg = new ResponseMessage<>(); | |
msg.message = message; | |
msg.status(status); | |
return msg.putTimeStamp(); | |
} | |
public static <T> ResponseMessage<T> ok() { | |
return ok(null); | |
} | |
private ResponseMessage<T> putTimeStamp() { | |
this.timestamp = System.currentTimeMillis(); | |
return this; | |
} | |
public static <T> ResponseMessage<T> ok(T result) { | |
return new ResponseMessage<T>() | |
.result(result) | |
.putTimeStamp() | |
.status(200); | |
} | |
public ResponseMessage<T> result(T result) { | |
this.result = result; | |
return this; | |
} | |
public ResponseMessage<T> code(String code) { | |
this.code = code; | |
return this; | |
} | |
/** | |
* 过滤字段:指定需要序列化的字段 | |
*/ | |
private transient Map<Class<?>, Set<String>> includes; | |
/** | |
* 过滤字段:指定不需要序列化的字段 | |
*/ | |
private transient Map<Class<?>, Set<String>> excludes; | |
public ResponseMessage() { | |
} | |
public ResponseMessage<T> include(Class<?> type, String... fields) { | |
return include(type, Arrays.asList(fields)); | |
} | |
public ResponseMessage<T> 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 (Exception ignore) { | |
} | |
} else { | |
getStringListFromMap(includes, type).add(field); | |
} | |
}); | |
return this; | |
} | |
public ResponseMessage<T> 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 (Exception ignore) { | |
} | |
} else { | |
getStringListFromMap(excludes, type).add(field); | |
} | |
}); | |
return this; | |
} | |
public ResponseMessage<T> exclude(Collection<String> fields) { | |
if (excludes == null) { | |
excludes = new HashMap<>(); | |
} | |
if (fields == null || fields.isEmpty()) { | |
return this; | |
} | |
Class type; | |
if (getResult() != null) { | |
type = getResult().getClass(); | |
} else { | |
return this; | |
} | |
exclude(type, fields); | |
return this; | |
} | |
public ResponseMessage<T> include(Collection<String> fields) { | |
if (includes == null) { | |
includes = new HashMap<>(); | |
} | |
if (fields == null || fields.isEmpty()) { | |
return this; | |
} | |
Class type; | |
if (getResult() != null) { | |
type = getResult().getClass(); | |
} else { | |
return this; | |
} | |
include(type, fields); | |
return this; | |
} | |
public ResponseMessage<T> exclude(Class type, String... fields) { | |
return exclude(type, Arrays.asList(fields)); | |
} | |
public ResponseMessage<T> exclude(String... fields) { | |
return exclude(Arrays.asList(fields)); | |
} | |
public ResponseMessage<T> include(String... fields) { | |
return include(Arrays.asList(fields)); | |
} | |
protected Set<String> getStringListFromMap(Map<Class<?>, Set<String>> map, Class type) { | |
return map.computeIfAbsent(type, k -> new HashSet<>()); | |
} | |
@Override | |
public String toString() { | |
return JSON.toJSONStringWithDateFormat(this, "yyyy-MM-dd HH:mm:ss"); | |
} | |
public ResponseMessage<T> status(int status) { | |
this.status = status; | |
return this; | |
} | |
public Map<Class<?>, Set<String>> getExcludes() { | |
return excludes; | |
} | |
public Map<Class<?>, Set<String>> getIncludes() { | |
return includes; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
public void setResult(T result) { | |
this.result = result; | |
} | |
public void setStatus(int status) { | |
this.status = status; | |
} | |
public void setTimestamp(Long timestamp) { | |
this.timestamp = timestamp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment