微信支付模块配置中增加ifSaveApiData参数,可以选择是否保存接口请求信息到ThreadLocal中方便读取

This commit is contained in:
Binary Wang 2018-11-11 21:16:47 +08:00
parent a5c8f13fff
commit 6720960d54
3 changed files with 26 additions and 10 deletions

View File

@ -88,6 +88,13 @@ public class WxPayConfig {
* 默认不使用
*/
private boolean useSandboxEnv = false;
/**
* 是否将接口请求日志信息保存到threadLocal中.
* 默认不保存
*/
private boolean ifSaveApiData = false;
private String httpProxyHost;
private Integer httpProxyPort;
private String httpProxyUsername;

View File

@ -1,8 +1,9 @@
package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.exception.WxPayException;
import jodd.util.Base64;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import javax.net.ssl.SSLContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
@ -20,9 +21,9 @@ import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.exception.WxPayException;
import jodd.util.Base64;
/**
* <pre>
@ -65,7 +66,9 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
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));
if (this.getConfig().isIfSaveApiData()) {
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
}
return responseString;
}
} finally {
@ -73,7 +76,9 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
}
} catch (Exception e) {
this.log.error("\n【请求地址】{}\n【请求数据】{}\n【异常信息】{}", url, requestStr, e.getMessage());
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
if (this.getConfig().isIfSaveApiData()) {
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
}
throw new WxPayException(e.getMessage(), e);
}
}

View File

@ -30,7 +30,9 @@ public class WxPayServiceJoddHttpImpl extends BaseWxPayServiceImpl {
byte[] responseBytes = request.send().bodyBytes();
final String responseString = Base64.encodeToString(responseBytes);
this.log.info("\n【请求地址】{}\n【请求数据】{}\n【响应数据(Base64编码后)】:{}", url, requestStr, responseString);
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
if (this.getConfig().isIfSaveApiData()) {
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
}
return responseBytes;
} catch (Exception e) {
this.log.error("\n【请求地址】{}\n【请求数据】{}\n【异常信息】{}", url, requestStr, e.getMessage());
@ -46,7 +48,9 @@ public class WxPayServiceJoddHttpImpl extends BaseWxPayServiceImpl {
String responseString = this.getResponseString(request.send());
this.log.info("\n【请求地址】{}\n【请求数据】{}\n【响应数据】{}", url, requestStr, responseString);
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
if (this.getConfig().isIfSaveApiData()) {
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
}
return responseString;
} catch (Exception e) {
this.log.error("\n【请求地址】{}\n【请求数据】{}\n【异常信息】{}", url, requestStr, e.getMessage());