Browse Source

修复由于静态引用SimpleDateFormat导致线程不安全的代码

master
BinaryWang 8 years ago
parent
commit
f66c0b02e2
6 changed files with 102 additions and 71 deletions
  1. +14
    -6
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java
  2. +46
    -39
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpDataCubeServiceImpl.java
  3. +4
    -2
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxLongTimeJsonSerializer.java
  4. +13
    -6
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpUserCumulateGsonAdapter.java
  5. +13
    -6
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpUserSummaryGsonAdapter.java
  6. +12
    -12
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpDataCubeServiceImplTest.java

+ 14
- 6
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java View File

@@ -3,18 +3,26 @@ package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.bean.WxJsapiSignature; import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor; import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;

import java.text.SimpleDateFormat;
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
import me.chanjar.weixin.mp.bean.WxMpIndustry;
import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage;
import me.chanjar.weixin.mp.bean.WxMpMassNews;
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage;
import me.chanjar.weixin.mp.bean.WxMpMassVideo;
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
import me.chanjar.weixin.mp.bean.result.WxMpUser;


/** /**
* 微信API的Service * 微信API的Service
*/ */
public interface WxMpService { public interface WxMpService {


SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

/** /**
* <pre> * <pre>
* 验证推送过来的消息的正确性 * 验证推送过来的消息的正确性


+ 46
- 39
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpDataCubeServiceImpl.java View File

@@ -1,8 +1,17 @@
package me.chanjar.weixin.mp.api.impl; package me.chanjar.weixin.mp.api.impl;


import java.text.Format;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;

import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpDataCubeService; import me.chanjar.weixin.mp.api.WxMpDataCubeService;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
@@ -13,11 +22,6 @@ import me.chanjar.weixin.mp.bean.datacube.WxDataCubeMsgResult;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate; import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary; import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;


/** /**
* Created by Binary Wang on 2016/8/23. * Created by Binary Wang on 2016/8/23.
@@ -27,6 +31,9 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
protected final Logger log = LoggerFactory.getLogger(WxMpDataCubeServiceImpl.class); protected final Logger log = LoggerFactory.getLogger(WxMpDataCubeServiceImpl.class);


private static final String API_URL_PREFIX = "https://api.weixin.qq.com/datacube"; private static final String API_URL_PREFIX = "https://api.weixin.qq.com/datacube";

private final Format dateFormat = FastDateFormat.getInstance("yyyy-MM-dd");

private WxMpService wxMpService; private WxMpService wxMpService;


public WxMpDataCubeServiceImpl(WxMpService wxMpService) { public WxMpDataCubeServiceImpl(WxMpService wxMpService) {
@@ -37,8 +44,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
public List<WxDataCubeUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException { public List<WxDataCubeUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getusersummary"; String url = API_URL_PREFIX + "/getusersummary";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -50,8 +57,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
public List<WxDataCubeUserCumulate> getUserCumulate(Date beginDate, Date endDate) throws WxErrorException { public List<WxDataCubeUserCumulate> getUserCumulate(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getusercumulate"; String url = API_URL_PREFIX + "/getusercumulate";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -63,8 +70,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
public List<WxDataCubeArticleResult> getArticleSummary(Date beginDate, Date endDate) throws WxErrorException { public List<WxDataCubeArticleResult> getArticleSummary(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getarticlesummary"; String url = API_URL_PREFIX + "/getarticlesummary";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -76,8 +83,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
public List<WxDataCubeArticleTotal> getArticleTotal(Date beginDate, Date endDate) throws WxErrorException { public List<WxDataCubeArticleTotal> getArticleTotal(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getarticletotal"; String url = API_URL_PREFIX + "/getarticletotal";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -89,8 +96,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
public List<WxDataCubeArticleResult> getUserRead(Date beginDate, Date endDate) throws WxErrorException { public List<WxDataCubeArticleResult> getUserRead(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getuserread"; String url = API_URL_PREFIX + "/getuserread";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -102,8 +109,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
public List<WxDataCubeArticleResult> getUserReadHour(Date beginDate, Date endDate) throws WxErrorException { public List<WxDataCubeArticleResult> getUserReadHour(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getuserreadhour"; String url = API_URL_PREFIX + "/getuserreadhour";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -115,8 +122,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
public List<WxDataCubeArticleResult> getUserShare(Date beginDate, Date endDate) throws WxErrorException { public List<WxDataCubeArticleResult> getUserShare(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getusershare"; String url = API_URL_PREFIX + "/getusershare";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -128,8 +135,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
public List<WxDataCubeArticleResult> getUserShareHour(Date beginDate, Date endDate) throws WxErrorException { public List<WxDataCubeArticleResult> getUserShareHour(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getusersharehour"; String url = API_URL_PREFIX + "/getusersharehour";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -142,8 +149,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
throws WxErrorException { throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsg"; String url = API_URL_PREFIX + "/getupstreammsg";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -156,8 +163,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
Date endDate) throws WxErrorException { Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsghour"; String url = API_URL_PREFIX + "/getupstreammsghour";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -170,8 +177,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
Date endDate) throws WxErrorException { Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsgweek"; String url = API_URL_PREFIX + "/getupstreammsgweek";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -184,8 +191,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
Date endDate) throws WxErrorException { Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsgmonth"; String url = API_URL_PREFIX + "/getupstreammsgmonth";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -198,8 +205,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
Date endDate) throws WxErrorException { Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsgdist"; String url = API_URL_PREFIX + "/getupstreammsgdist";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -212,8 +219,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
Date endDate) throws WxErrorException { Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsgdistweek"; String url = API_URL_PREFIX + "/getupstreammsgdistweek";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -226,8 +233,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
Date endDate) throws WxErrorException { Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsgdistmonth"; String url = API_URL_PREFIX + "/getupstreammsgdistmonth";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -240,8 +247,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
Date endDate) throws WxErrorException { Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getinterfacesummary"; String url = API_URL_PREFIX + "/getinterfacesummary";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
@@ -254,8 +261,8 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
Date endDate) throws WxErrorException { Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getinterfacesummaryhour"; String url = API_URL_PREFIX + "/getinterfacesummaryhour";
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString()); String responseContent = this.wxMpService.post(url, param.toString());
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent); this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, param, responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),


+ 4
- 2
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxLongTimeJsonSerializer.java View File

@@ -1,7 +1,9 @@
package me.chanjar.weixin.mp.util.json; package me.chanjar.weixin.mp.util.json;


import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.text.Format;

import org.apache.commons.lang3.time.FastDateFormat;


import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.JsonSerializer;
@@ -11,7 +13,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
* Created by Binary Wang on 2016/7/13. * Created by Binary Wang on 2016/7/13.
*/ */
public class WxLongTimeJsonSerializer extends JsonSerializer<Long> { public class WxLongTimeJsonSerializer extends JsonSerializer<Long> {
private static SimpleDateFormat DF = new SimpleDateFormat(
private static Format DF = FastDateFormat.getInstance(
"yyyy-MM-dd HH:mm:ss"); "yyyy-MM-dd HH:mm:ss");


@Override @Override


+ 13
- 6
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpUserCumulateGsonAdapter.java View File

@@ -8,13 +8,19 @@
*/ */
package me.chanjar.weixin.mp.util.json; package me.chanjar.weixin.mp.util.json;


import com.google.gson.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate;

import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.commons.lang3.time.FastDateFormat;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate;


/** /**
* *
@@ -23,7 +29,8 @@ import java.text.SimpleDateFormat;
*/ */
public class WxMpUserCumulateGsonAdapter implements JsonDeserializer<WxDataCubeUserCumulate> { public class WxMpUserCumulateGsonAdapter implements JsonDeserializer<WxDataCubeUserCumulate> {


private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
private static final FastDateFormat SIMPLE_DATE_FORMAT = FastDateFormat
.getInstance("yyyy-MM-dd");


@Override @Override
public WxDataCubeUserCumulate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public WxDataCubeUserCumulate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {


+ 13
- 6
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpUserSummaryGsonAdapter.java View File

@@ -8,20 +8,27 @@
*/ */
package me.chanjar.weixin.mp.util.json; package me.chanjar.weixin.mp.util.json;


import com.google.gson.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;

import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.commons.lang3.time.FastDateFormat;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;


/** /**
* @author Daniel Qian * @author Daniel Qian
*/ */
public class WxMpUserSummaryGsonAdapter implements JsonDeserializer<WxDataCubeUserSummary> { public class WxMpUserSummaryGsonAdapter implements JsonDeserializer<WxDataCubeUserSummary> {


private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
private static final FastDateFormat SIMPLE_DATE_FORMAT = FastDateFormat
.getInstance("yyyy-MM-dd");


@Override @Override
public WxDataCubeUserSummary deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) public WxDataCubeUserSummary deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)


+ 12
- 12
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpDataCubeServiceImplTest.java View File

@@ -1,10 +1,10 @@
package me.chanjar.weixin.mp.api.impl; package me.chanjar.weixin.mp.api.impl;


import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;


import org.apache.commons.lang3.time.FastDateFormat;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice; import org.testng.annotations.Guice;
@@ -28,39 +28,39 @@ import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
*/ */
@Guice(modules = ApiTestModule.class) @Guice(modules = ApiTestModule.class)
public class WxMpDataCubeServiceImplTest { public class WxMpDataCubeServiceImplTest {
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
private FastDateFormat simpleDateFormat = FastDateFormat
.getInstance("yyyy-MM-dd");


@Inject @Inject
protected WxMpServiceImpl wxService; protected WxMpServiceImpl wxService;


@DataProvider @DataProvider
public Object[][] oneDay() throws ParseException { public Object[][] oneDay() throws ParseException {
return new Object[][] { { simpleDateFormat.parse("2016-08-22") } };
return new Object[][] { { this.simpleDateFormat.parse("2016-08-22") } };
} }


@DataProvider @DataProvider
public Object[][] threeDays() throws ParseException { public Object[][] threeDays() throws ParseException {
return new Object[][] { { simpleDateFormat.parse("2016-08-20"),
simpleDateFormat.parse("2016-08-22") } };
return new Object[][] { { this.simpleDateFormat.parse("2016-08-20"),
this.simpleDateFormat.parse("2016-08-22") } };
} }


@DataProvider @DataProvider
public Object[][] sevenDays() throws ParseException { public Object[][] sevenDays() throws ParseException {
return new Object[][] { { simpleDateFormat.parse("2016-08-16"),
simpleDateFormat.parse("2016-08-22") } };
return new Object[][] { { this.simpleDateFormat.parse("2016-08-16"),
this.simpleDateFormat.parse("2016-08-22") } };
} }


@DataProvider @DataProvider
public Object[][] fifteenDays() throws ParseException { public Object[][] fifteenDays() throws ParseException {
return new Object[][] { { simpleDateFormat.parse("2016-08-14"),
simpleDateFormat.parse("2016-08-27") } };
return new Object[][] { { this.simpleDateFormat.parse("2016-08-14"),
this.simpleDateFormat.parse("2016-08-27") } };
} }


@DataProvider @DataProvider
public Object[][] thirtyDays() throws ParseException { public Object[][] thirtyDays() throws ParseException {
return new Object[][] { { simpleDateFormat.parse("2016-07-30"),
simpleDateFormat.parse("2016-08-27") } };
return new Object[][] { { this.simpleDateFormat.parse("2016-07-30"),
this.simpleDateFormat.parse("2016-08-27") } };
} }


@Test(dataProvider = "sevenDays") @Test(dataProvider = "sevenDays")


Loading…
Cancel
Save