public abstract class BaseResponse<T> {
private boolean success = true;
private Map<String, String> errorMsg = new HashMap();
private List<T> data;
public BaseResponse() {
}
public List<T> getData() {
return this.data;
}
public void setData(List<T> data) {
this.data = data;
}
public boolean isSuccess() {
return this.success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public Map<String, String> getErrorMsg() {
return this.errorMsg;
}
public void setErrorMsg(Map<String, String> errorMsg) {
this.errorMsg = errorMsg;
}
public Set<String> errorCodes() {
return this.errorMsg.keySet();
}
public Collection<String> errorMsgs() {
return this.errorMsg.values();
}
public void addError(String code, String msg) {
this.setSuccess(false);
this.errorMsg.put(code, msg);
}
public void addError(Map<String, String> errorMsgMap) {
this.setSuccess(false);
this.errorMsg.putAll(errorMsgMap);
}
public T getSingleData() {
return this.data != null && !this.data.isEmpty()?this.data.get(0):null;
}
public void setData(T data) {
if(this.data == null) {
this.data = new ArrayList();
}
this.data.add(data);
}
public String buildMsg(String sep) {
StringBuilder sb = new StringBuilder();
if(!this.errorMsg.isEmpty()) {
Iterator var3 = this.errorMsg.entrySet().iterator();
while(var3.hasNext()) {
Entry entry = (Entry)var3.next();
sb.append((String)entry.getKey()).append(":");
sb.append((String)entry.getValue()).append(sep);
}
}
return sb.toString();
}
public String toString() {
return "result:" + this.success + ";" + this.buildMsg(";") + "data:" + (this.getData() != null?ToStringBuilder.reflectionToString(this.getData()):"");
}
}
public class StringResponse extends BaseResponse<String> {
public StringResponse() {
}
public StringResponse(String data) {
this.setData(data);
}
public StringResponse(List<String> dataList) {
this.setData(dataList);
}
}
运行
public class Test {
public static void main(String[] args) {
String str = "{\"success\":true,\"errorMsg\":{},\"data\":[\"11111111111112222\"],\"singleData\":\"11111111111112222\"}";
StringResponse result = JSON.parseObject(str, StringResponse.class);
JSONObject o = JSON.parseObject(str);
System.out.println(result.getSingleData());
}
}
6条答案
按热度按时间piok6c0g1#
版本 1.2.8
deyfvvtc2#
能提供重现问题的代码么?
e37o9pze3#
json:
{\"success\":true,\"errorMsg\":{},\"data\":[\"111118\"],\"singleData\":\"444444441dsadasdasd\"}
parent abstract class:
运行
44u64gxh4#
多运行几次 会出现差别
3hvapo4f5#
@wenshao 代码已经给出了
jtoj6r0c6#
请问解决了没啊,这父类泛型反序列化能否支持,但是当前子类是没有泛型的。。。。