1
0
mirror of synced 2025-12-18 05:47:58 +08:00

优化代码,替换掉ToStringBuilder.reflectionToString相关代码

This commit is contained in:
Binary Wang
2018-11-02 11:18:56 +08:00
parent cd28a5b487
commit eab7dd398a
138 changed files with 670 additions and 731 deletions

View File

@@ -56,7 +56,7 @@ public class WxCpDepartmentServiceImpl implements WxCpDepartmentService {
String responseContent = this.mainService.get(url, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.INSTANCE.create()
return WxCpGsonBuilder.create()
.fromJson(tmpJsonElement.getAsJsonObject().get("department"),
new TypeToken<List<WxCpDepart>>() {
}.getType()

View File

@@ -1,6 +1,5 @@
package me.chanjar.weixin.cp.api.impl;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@@ -11,6 +10,7 @@ import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpUserDetail;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
/**
* <pre>
@@ -87,6 +87,6 @@ public class WxCpOAuth2ServiceImpl implements WxCpOAuth2Service {
JsonObject param = new JsonObject();
param.addProperty("user_ticket", userTicket);
String responseText = this.mainService.post(url, param.toString());
return new GsonBuilder().create().fromJson(responseText, WxCpUserDetail.class);
return WxCpGsonBuilder.create().fromJson(responseText, WxCpUserDetail.class);
}
}

View File

@@ -1,6 +1,12 @@
package me.chanjar.weixin.cp.api.impl;
import com.google.gson.*;
import java.util.List;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
@@ -11,8 +17,6 @@ import me.chanjar.weixin.cp.bean.WxCpTagGetResult;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.util.List;
/**
* <pre>
* 标签管理接口
@@ -57,7 +61,7 @@ public class WxCpTagServiceImpl implements WxCpTagService {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list";
String responseContent = this.mainService.get(url, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.INSTANCE.create()
return WxCpGsonBuilder.create()
.fromJson(
tmpJsonElement.getAsJsonObject().get("taglist"),
new TypeToken<List<WxCpTag>>() {
@@ -70,7 +74,7 @@ public class WxCpTagServiceImpl implements WxCpTagService {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?tagid=" + tagId;
String responseContent = this.mainService.get(url, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.INSTANCE.create()
return WxCpGsonBuilder.create()
.fromJson(
tmpJsonElement.getAsJsonObject().get("userlist"),
new TypeToken<List<WxCpUser>>() {

View File

@@ -90,7 +90,7 @@ public class WxCpUserServiceImpl implements WxCpUserService {
String responseContent = this.mainService.get(url, params);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.INSTANCE.create()
return WxCpGsonBuilder.create()
.fromJson(tmpJsonElement.getAsJsonObject().get("userlist"),
new TypeToken<List<WxCpUser>>() {
}.getType()
@@ -112,7 +112,7 @@ public class WxCpUserServiceImpl implements WxCpUserService {
String responseContent = this.mainService.get(url, params);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.INSTANCE.create()
return WxCpGsonBuilder.create()
.fromJson(
tmpJsonElement.getAsJsonObject().get("userlist"),
new TypeToken<List<WxCpUser>>() {

View File

@@ -1,5 +1,8 @@
package me.chanjar.weixin.cp.bean;
import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -7,9 +10,6 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.io.Serializable;
import java.util.List;
/**
* <pre>
* 企业号应用信息.
@@ -80,12 +80,14 @@ public class WxCpAgent implements Serializable {
@Data
public static class Users implements Serializable {
private static final long serialVersionUID = 8801100463558788565L;
@SerializedName("user")
private List<User> users;
}
@Data
public class User implements Serializable {
private static final long serialVersionUID = 7287632514385508024L;
@SerializedName("userid")
private String userId;
}

View File

@@ -5,8 +5,6 @@ import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.google.common.base.Splitter;
import com.google.gson.annotations.SerializedName;
@@ -25,11 +23,11 @@ public class WxCpInviteResult implements Serializable {
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
return WxCpGsonBuilder.create().toJson(this);
}
public static WxCpInviteResult fromJson(String json) {
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpInviteResult.class);
return WxCpGsonBuilder.create().fromJson(json, WxCpInviteResult.class);
}
@SerializedName("errcode")

View File

@@ -1,16 +1,23 @@
package me.chanjar.weixin.cp.bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
import me.chanjar.weixin.cp.bean.article.NewArticle;
import me.chanjar.weixin.cp.bean.messagebuilder.*;
import me.chanjar.weixin.cp.bean.messagebuilder.FileBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.ImageBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.MpnewsBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.NewsBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.TextBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.TextCardBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.VideoBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.VoiceBuilder;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* 消息.
*
@@ -114,7 +121,7 @@ public class WxCpMessage implements Serializable {
}
public String toJson() {
return WxCpGsonBuilder.INSTANCE.create().toJson(this);
return WxCpGsonBuilder.create().toJson(this);
}
}

View File

@@ -5,8 +5,6 @@ import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.google.common.base.Splitter;
import com.google.gson.annotations.SerializedName;
@@ -25,11 +23,11 @@ public class WxCpMessageSendResult implements Serializable {
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
return WxCpGsonBuilder.create().toJson(this);
}
public static WxCpMessageSendResult fromJson(String json) {
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpMessageSendResult.class);
return WxCpGsonBuilder.create().fromJson(json, WxCpMessageSendResult.class);
}
@SerializedName("errcode")

View File

@@ -1,14 +1,15 @@
package me.chanjar.weixin.cp.bean;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.io.Serializable;
/**
* Created by Daniel Qian.
* @author Daniel Qian
*/
@Data
@AllArgsConstructor

View File

@@ -5,8 +5,6 @@ import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.google.common.base.Splitter;
import com.google.gson.annotations.SerializedName;
@@ -25,11 +23,11 @@ public class WxCpTagAddOrRemoveUsersResult implements Serializable {
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
return WxCpGsonBuilder.create().toJson(this);
}
public static WxCpTagAddOrRemoveUsersResult fromJson(String json) {
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpTagAddOrRemoveUsersResult.class);
return WxCpGsonBuilder.create().fromJson(json, WxCpTagAddOrRemoveUsersResult.class);
}
@SerializedName("errcode")

View File

@@ -50,11 +50,11 @@ public class WxCpUser implements Serializable {
}
public static WxCpUser fromJson(String json) {
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpUser.class);
return WxCpGsonBuilder.create().fromJson(json, WxCpUser.class);
}
public String toJson() {
return WxCpGsonBuilder.INSTANCE.create().toJson(this);
return WxCpGsonBuilder.create().toJson(this);
}
@Data

View File

@@ -7,8 +7,6 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
@@ -18,6 +16,7 @@ import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import me.chanjar.weixin.cp.util.xml.XStreamTransformer;
/**
@@ -378,7 +377,7 @@ public class WxCpXmlMessage implements Serializable {
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
return WxCpGsonBuilder.create().toJson(this);
}
@Data

View File

@@ -2,11 +2,9 @@ package me.chanjar.weixin.cp.config;
import java.io.File;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
/**
* 基于内存的微信配置provider在实际生产环境中应该将这些配置持久化
@@ -203,7 +201,7 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
return WxCpGsonBuilder.create().toJson(this);
}
@Override

View File

@@ -15,7 +15,7 @@ import me.chanjar.weixin.cp.bean.WxCpUser;
*/
public class WxCpGsonBuilder {
public static final GsonBuilder INSTANCE = new GsonBuilder();
private static final GsonBuilder INSTANCE = new GsonBuilder();
static {
INSTANCE.disableHtmlEscaping();