🎨 #1659 Json解析统一优化

* 解决动态添加第一个公众号时由于configStorageMap为null报空指针异常

* Json解析统一优化

Co-authored-by: niefy <niefy@qq.com>
This commit is contained in:
niefy
2020-07-06 17:01:37 +08:00
committed by GitHub
parent 8121a521d3
commit 0adca9650d
114 changed files with 229 additions and 337 deletions

View File

@@ -3,9 +3,7 @@ package me.chanjar.weixin.mp.api.impl;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@@ -23,6 +21,7 @@ import me.chanjar.weixin.common.util.DataUtils;
import me.chanjar.weixin.common.util.RandomUtils;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.common.util.http.*;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
@@ -48,7 +47,7 @@ import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.*;
*/
@Slf4j
public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestHttp<H, P> {
private static final JsonParser JSON_PARSER = new JsonParser();
protected WxSessionManager sessionManager = new StandardSessionManager();
private WxMpKefuService kefuService = new WxMpKefuServiceImpl(this);
@@ -111,7 +110,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
if (this.getWxMpConfigStorage().isTicketExpired(type)) {
String responseContent = execute(SimpleGetRequestExecutor.create(this),
GET_TICKET_URL.getUrl(this.getWxMpConfigStorage()) + type.getCode(), null);
JsonObject tmpJsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
this.getWxMpConfigStorage().updateTicket(type, jsapiTicket, expiresInSeconds);
@@ -166,8 +165,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
o.addProperty("action", "long2short");
o.addProperty("long_url", longUrl);
String responseContent = this.post(SHORTURL_API_URL, o.toString());
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
return GsonParser.parse(responseContent).get("short_url").getAsString();
}
@Override
@@ -245,8 +243,8 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
@Override
public String[] getCallbackIP() throws WxErrorException {
String responseContent = this.get(GET_CALLBACK_IP_URL, null);
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
JsonArray ipList = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray();
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
JsonArray ipList = tmpJsonObject.get("ip_list").getAsJsonArray();
String[] ipArray = new String[ipList.size()];
for (int i = 0; i < ipList.size(); i++) {
ipArray[i] = ipList.get(i).getAsString();
@@ -434,7 +432,11 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
@Override
public void addConfigStorage(String mpId, WxMpConfigStorage configStorages) {
synchronized (this) {
this.configStorageMap.put(mpId, configStorages);
if (this.configStorageMap == null) {
this.setWxMpConfigStorage(configStorages);
} else {
this.configStorageMap.put(mpId, configStorages);
}
}
}

View File

@@ -1,10 +1,10 @@
package me.chanjar.weixin.mp.api.impl;
import com.google.gson.JsonParser;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.mp.api.WxMpAiOpenService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.enums.AiLangType;
@@ -23,7 +23,7 @@ import static me.chanjar.weixin.mp.enums.WxMpApiUrl.AiOpen.*;
*/
@RequiredArgsConstructor
public class WxMpAiOpenServiceImpl implements WxMpAiOpenService {
private static final JsonParser JSON_PARSER = new JsonParser();
private final WxMpService wxMpService;
@Override
@@ -53,7 +53,7 @@ public class WxMpAiOpenServiceImpl implements WxMpAiOpenService {
throw new WxErrorException(error);
}
return JSON_PARSER.parse(response).getAsJsonObject().get("to_content").getAsString();
return GsonParser.parse(response).get("to_content").getAsString();
}
@Override
@@ -69,6 +69,6 @@ public class WxMpAiOpenServiceImpl implements WxMpAiOpenService {
throw new WxErrorException(error);
}
return JSON_PARSER.parse(response).getAsJsonObject().get("result").getAsString();
return GsonParser.parse(response).get("result").getAsString();
}
}

View File

@@ -9,6 +9,7 @@ import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.RandomUtils;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.api.WxMpCardService;
import me.chanjar.weixin.mp.api.WxMpService;
@@ -58,8 +59,7 @@ public class WxMpCardServiceImpl implements WxMpCardService {
if (this.getWxMpService().getWxMpConfigStorage().isTicketExpired(type)) {
String responseContent = this.wxMpService.execute(SimpleGetRequestExecutor
.create(this.getWxMpService().getRequestHttp()), WxMpApiUrl.Card.CARD_GET_TICKET, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
String cardApiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
this.getWxMpService().getWxMpConfigStorage().updateTicket(type, cardApiTicket, expiresInSeconds);
@@ -99,8 +99,7 @@ public class WxMpCardServiceImpl implements WxMpCardService {
JsonObject param = new JsonObject();
param.addProperty("encrypt_code", encryptCode);
String responseContent = this.wxMpService.post(WxMpApiUrl.Card.CARD_CODE_DECRYPT, param.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
JsonPrimitive jsonPrimitive = tmpJsonObject.getAsJsonPrimitive("code");
return jsonPrimitive.getAsString();
}
@@ -159,7 +158,7 @@ public class WxMpCardServiceImpl implements WxMpCardService {
String responseContent = this.wxMpService.post(WxMpApiUrl.Card.CARD_GET, param.toString());
// 判断返回值
JsonObject json = (new JsonParser()).parse(responseContent).getAsJsonObject();
JsonObject json = GsonParser.parse(responseContent);
String errcode = json.get("errcode").getAsString();
if (!"0".equals(errcode)) {
String errmsg = json.get("errmsg").getAsString();

View File

@@ -6,7 +6,6 @@ import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpDeviceService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.device.*;
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Device.*;

View File

@@ -1,12 +1,11 @@
package me.chanjar.weixin.mp.api.impl;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.mp.api.WxMpMarketingService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.marketing.WxMpAdLeadFilter;
@@ -38,8 +37,8 @@ public class WxMpMarketingServiceImpl implements WxMpMarketingService {
json.addProperty("name", name);
json.addProperty("description", description);
String responseContent = wxMpService.post(USER_ACTION_SETS_ADD, json.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return tmpJsonElement.getAsJsonObject().get("data").getAsJsonObject().get("user_action_set_id").getAsLong();
JsonObject tmpJson = GsonParser.parse(responseContent);
return tmpJson.get("data").getAsJsonObject().get("user_action_set_id").getAsLong();
}
@Override

View File

@@ -11,7 +11,6 @@ import me.chanjar.weixin.mp.bean.result.WxMpMassGetResult;
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
import me.chanjar.weixin.mp.bean.result.WxMpMassSpeedGetResult;
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.*;

View File

@@ -13,7 +13,6 @@ import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.api.WxMpMaterialService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.material.*;
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import me.chanjar.weixin.mp.util.requestexecuter.material.*;
import me.chanjar.weixin.mp.util.requestexecuter.media.MediaImgUploadRequestExecutor;

View File

@@ -1,11 +1,11 @@
package me.chanjar.weixin.mp.api.impl;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.mp.api.WxMpMenuService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
@@ -38,7 +38,7 @@ public class WxMpMenuServiceImpl implements WxMpMenuService {
log.debug("创建菜单:{},结果:{}", menuJson, result);
if (menu.getMatchRule() != null) {
return new JsonParser().parse(result).getAsJsonObject().get("menuid").getAsString();
return GsonParser.parse(result).get("menuid").getAsString();
}
return null;
@@ -46,8 +46,7 @@ public class WxMpMenuServiceImpl implements WxMpMenuService {
@Override
public String menuCreate(String json) throws WxErrorException {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(json).getAsJsonObject();
JsonObject jsonObject = GsonParser.parse(json);
WxMpApiUrl.Menu url = MENU_CREATE;
if (jsonObject.get("matchrule") != null) {
url = MENU_ADDCONDITIONAL;
@@ -55,7 +54,7 @@ public class WxMpMenuServiceImpl implements WxMpMenuService {
String result = this.wxMpService.post(url, json);
if (jsonObject.get("matchrule") != null) {
return jsonParser.parse(result).getAsJsonObject().get("menuid").getAsString();
return GsonParser.parse(result).get("menuid").getAsString();
}
return null;

View File

@@ -1,13 +1,13 @@
package me.chanjar.weixin.mp.api.impl;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.BeanUtils;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpStoreService;
import me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo;
@@ -47,7 +47,7 @@ public class WxMpStoreServiceImpl implements WxMpStoreService {
if (wxError.getErrorCode() != 0) {
throw new WxErrorException(wxError);
}
return WxMpStoreBaseInfo.fromJson(new JsonParser().parse(response).getAsJsonObject()
return WxMpStoreBaseInfo.fromJson(GsonParser.parse(response)
.get("business").getAsJsonObject().get("base_info").toString());
}
@@ -117,7 +117,7 @@ public class WxMpStoreServiceImpl implements WxMpStoreService {
}
return WxMpGsonBuilder.create().fromJson(
new JsonParser().parse(response).getAsJsonObject().get("category_list"),
GsonParser.parse(response).get("category_list"),
new TypeToken<List<String>>() {
}.getType());
}

View File

@@ -1,17 +1,16 @@
package me.chanjar.weixin.mp.api.impl;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
import java.util.List;
@@ -26,14 +25,14 @@ import static me.chanjar.weixin.mp.enums.WxMpApiUrl.TemplateMsg.*;
*/
@RequiredArgsConstructor
public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService {
private static final JsonParser JSON_PARSER = new JsonParser();
private final WxMpService wxMpService;
@Override
public String sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErrorException {
String responseContent = this.wxMpService.post(MESSAGE_TEMPLATE_SEND, templateMessage.toJson());
final JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
final JsonObject jsonObject = GsonParser.parse(responseContent);
if (jsonObject.get("errcode").getAsInt() == 0) {
return jsonObject.get("msgid").getAsString();
}
@@ -62,7 +61,7 @@ public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("template_id_short", shortTemplateId);
String responseContent = this.wxMpService.post(TEMPLATE_API_ADD_TEMPLATE, jsonObject.toString());
final JsonObject result = JSON_PARSER.parse(responseContent).getAsJsonObject();
final JsonObject result = GsonParser.parse(responseContent);
if (result.get("errcode").getAsInt() == 0) {
return result.get("template_id").getAsString();
}

View File

@@ -7,7 +7,6 @@ import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserBlacklistService;
import me.chanjar.weixin.mp.bean.result.WxMpUserBlacklistGetResult;
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import java.util.HashMap;

View File

@@ -9,7 +9,6 @@ import me.chanjar.weixin.mp.bean.WxMpUserQuery;
import me.chanjar.weixin.mp.bean.result.WxMpChangeOpenid;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import java.util.HashMap;

View File

@@ -2,12 +2,12 @@ package me.chanjar.weixin.mp.api.impl;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserTagService;
import me.chanjar.weixin.mp.bean.tag.WxTagListUser;
@@ -134,7 +134,7 @@ public class WxMpUserTagServiceImpl implements WxMpUserTagService {
String responseContent = this.wxMpService.post(TAGS_GETIDLIST, json.toString());
return WxMpGsonBuilder.create().fromJson(
new JsonParser().parse(responseContent).getAsJsonObject().get("tagid_list"),
GsonParser.parse(responseContent).get("tagid_list"),
new TypeToken<List<Long>>() {
}.getType());
}