diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/WxPayApiData.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/WxPayApiData.java
new file mode 100644
index 00000000..ab1dec71
--- /dev/null
+++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/WxPayApiData.java
@@ -0,0 +1,87 @@
+package com.github.binarywang.wxpay.bean;
+
+/**
+ *
+ * 微信支付接口请求数据封装对象
+ * Created by Binary Wang on 2017-8-25.
+ *
+ *
+ * @author Binary Wang
+ */
+public class WxPayApiData {
+ /**
+ * 接口请求地址
+ */
+ private String url;
+
+ /**
+ * 请求数据
+ */
+ private String requestData;
+
+ /**
+ * 响应数据
+ */
+ private String responseData;
+
+ /**
+ * 接口请求异常信息
+ */
+ private String exceptionMsg;
+
+ /**
+ * @param url 接口请求地址
+ * @param requestData 请求数据
+ * @param responseData 响应数据
+ * @param exceptionMsg 接口请求异常信息
+ */
+ public WxPayApiData(String url, String requestData, String responseData, String exceptionMsg) {
+ this.url = url;
+ this.requestData = requestData;
+ this.responseData = responseData;
+ this.exceptionMsg = exceptionMsg;
+ }
+
+ public String getUrl() {
+ return this.url;
+ }
+
+ public void setUrl(String url) {
+ this.url = url;
+ }
+
+ public String getRequestData() {
+ return this.requestData;
+ }
+
+ public void setRequestData(String requestData) {
+ this.requestData = requestData;
+ }
+
+ public String getResponseData() {
+ return this.responseData;
+ }
+
+ public void setResponseData(String responseData) {
+ this.responseData = responseData;
+ }
+
+ public String getExceptionMsg() {
+ return this.exceptionMsg;
+ }
+
+ public void setExceptionMsg(String exceptionMsg) {
+ this.exceptionMsg = exceptionMsg;
+ }
+
+ @Override
+ public String toString() {
+ if (this.exceptionMsg != null) {
+ return String.format("\n【请求地址】:%s\n【请求数据】:%s\n【异常信息】:%s",
+ this, url, this.requestData, this.exceptionMsg);
+ }
+
+ return String.format("\n【请求地址】:%s\n【请求数据】:%s\n【响应数据】:%s",
+ this.url, this.requestData, this.responseData);
+ }
+}
diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java
index 2eef29b8..bf599ba1 100644
--- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java
+++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java
@@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.service;
+import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.bean.coupon.*;
import com.github.binarywang.wxpay.bean.request.*;
import com.github.binarywang.wxpay.bean.result.*;
@@ -368,4 +369,9 @@ public interface WxPayService {
*
*/
WxPayCouponInfoQueryResult queryCouponInfo(WxPayCouponInfoQueryRequest request) throws WxPayException;
+
+ /**
+ * 获取微信请求数据,方便接口调用方获取处理
+ */
+ WxPayApiData getWxApiData();
}
diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImpl.java
index 357cad09..27d4d2dd 100644
--- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImpl.java
+++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImpl.java
@@ -1,6 +1,7 @@
package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.utils.qrcode.QrcodeUtils;
+import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.bean.coupon.*;
import com.github.binarywang.wxpay.bean.request.*;
import com.github.binarywang.wxpay.bean.result.*;
@@ -33,6 +34,7 @@ import java.util.Map;
public abstract class WxPayServiceAbstractImpl implements WxPayService {
private static final String PAY_BASE_URL = "https://api.mch.weixin.qq.com";
protected final Logger log = LoggerFactory.getLogger(this.getClass());
+ protected static ThreadLocal wxApiData = new ThreadLocal<>();
protected WxPayConfig config;
@@ -501,4 +503,15 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
result.checkResult(this);
return result;
}
+
+ @Override
+ public WxPayApiData getWxApiData() {
+ try {
+ return wxApiData.get();
+ }finally {
+ //一般来说,接口请求会在一个线程内进行,这种情况下,每个线程get的会是之前所存入的数据,
+ // 但以防万一有同一线程多次请求的问题,所以每次获取完数据后移除对应数据
+ wxApiData.remove();
+ }
+ }
}
diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java
index 7d8dfcf7..1b3284f9 100644
--- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java
+++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java
@@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.service.impl;
+import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.exception.WxPayException;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.auth.AuthScope;
@@ -66,15 +67,17 @@ public class WxPayServiceApacheHttpImpl extends WxPayServiceAbstractImpl {
try (CloseableHttpClient httpclient = httpClientBuilder.build()) {
httpPost.setEntity(new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)));
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
- String result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
- this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, result);
- return result;
+ String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
+ this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
+ wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
+ return responseString;
}
} finally {
httpPost.releaseConnection();
}
} catch (Exception e) {
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
+ wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
throw new WxPayException(e.getMessage(), e);
}
}
diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java
index 553d858c..bbe024a3 100644
--- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java
+++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java
@@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.service.impl;
+import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.exception.WxPayException;
import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
@@ -53,9 +54,11 @@ public class WxPayServiceJoddHttpImpl extends WxPayServiceAbstractImpl {
String responseString = this.getResponseString(request.send());
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
+ wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
return responseString;
} catch (Exception e) {
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
+ wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
throw new WxPayException(e.getMessage(), e);
}
}
diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java
index 6d110a0e..de0fe10e 100644
--- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java
+++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java
@@ -53,6 +53,7 @@ public class WxPayServiceAbstractImplTest {
.outTradeNo("1111112")
.build());
this.logger.info(result.toString());
+ this.logger.warn(this.payService.getWxApiData().toString());
}
@Test
diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/testbase/ApiTestModule.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/testbase/ApiTestModule.java
index d5a028d1..207dc194 100644
--- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/testbase/ApiTestModule.java
+++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/testbase/ApiTestModule.java
@@ -12,11 +12,16 @@ import java.io.IOException;
import java.io.InputStream;
public class ApiTestModule implements Module {
+ private static final String TEST_CONFIG_XML = "test-config.xml";
@Override
public void configure(Binder binder) {
- try (InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml")) {
- XmlWxPayConfig config = this.fromXml(XmlWxPayConfig.class, is1);
+ try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) {
+ if (inputStream == null) {
+ throw new RuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到");
+ }
+
+ XmlWxPayConfig config = this.fromXml(XmlWxPayConfig.class, inputStream);
WxPayService wxService = new WxPayServiceImpl();
wxService.setConfig(config);