mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-16 13:26:23 +08:00
🎨 #1390 微信支付增加XML转换的快速模式,发送请求以及组装响应对象的时候不再依赖反射机制
* 增加XML的快速模式,发送请求以及组装响应对象的时候,不再依赖java的反射机制。 1:提升性能 2:可以通过 graalvm 生成native image. 本次完成:全部BaseWxPayRequest的改造,部分BaseWxPayResult子类的改造。 * clean code * 标记 xmlDoc 为 transient 否则toString()方法中Gson可能会堆栈溢出 * 完成大多数BaseWxPayResult子类的改造。还有 notify.*Result下面留了两个TODO需要处理。 * toXML时遗漏了sign参数 * 使用dom4j简化了toXML,同时根据本版本构建native-image的demo已经提交: https://github.com/outersky/wx-micronaut-graal.git 供参考。 * 完成了最后两个Result的xml解析。
This commit is contained in:
@@ -65,7 +65,18 @@ public class SignUtils {
|
||||
* @return 签名字符串 string
|
||||
*/
|
||||
public static String createSign(Object xmlBean, String signType, String signKey, String[] ignoredParams) {
|
||||
return createSign(xmlBean2Map(xmlBean), signType, signKey, ignoredParams);
|
||||
Map<String, String> map = null;
|
||||
|
||||
if (XmlConfig.fastMode) {
|
||||
if (xmlBean instanceof BaseWxPayRequest) {
|
||||
map = ((BaseWxPayRequest) xmlBean).getSignParams();
|
||||
}
|
||||
}
|
||||
if (map == null) {
|
||||
map = xmlBean2Map(xmlBean);
|
||||
}
|
||||
|
||||
return createSign(map, signType, signKey, ignoredParams);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,7 +102,7 @@ public class SignUtils {
|
||||
|
||||
if (shouldSign) {
|
||||
toSign.append(key).append("=").append(value).append("&");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
toSign.append("key=").append(signKey);
|
||||
@@ -104,25 +115,26 @@ public class SignUtils {
|
||||
|
||||
/**
|
||||
* 企业微信签名
|
||||
*
|
||||
* @param signType md5 目前接口要求使用的加密类型
|
||||
*/
|
||||
public static String createEntSign(String actName,String mchBillNo,String mchId,String nonceStr,
|
||||
String reOpenid,Integer totalAmount,String wxAppId,String signKey,
|
||||
String signType){
|
||||
public static String createEntSign(String actName, String mchBillNo, String mchId, String nonceStr,
|
||||
String reOpenid, Integer totalAmount, String wxAppId, String signKey,
|
||||
String signType) {
|
||||
Map<String, String> sortedMap = new HashMap<>();
|
||||
sortedMap.put("act_name",actName);
|
||||
sortedMap.put("mch_billno",mchBillNo);
|
||||
sortedMap.put("mch_id",mchId);
|
||||
sortedMap.put("nonce_str",nonceStr);
|
||||
sortedMap.put("re_openid",reOpenid);
|
||||
sortedMap.put("act_name", actName);
|
||||
sortedMap.put("mch_billno", mchBillNo);
|
||||
sortedMap.put("mch_id", mchId);
|
||||
sortedMap.put("nonce_str", nonceStr);
|
||||
sortedMap.put("re_openid", reOpenid);
|
||||
sortedMap.put("total_amount", totalAmount + "");
|
||||
sortedMap.put("wxappid",wxAppId);
|
||||
sortedMap.put("wxappid", wxAppId);
|
||||
|
||||
Map<String, String> sortParams = new TreeMap<>(sortedMap);
|
||||
Set<Map.Entry<String, String>> entries = sortParams.entrySet();
|
||||
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
|
||||
StringBuilder toSign = new StringBuilder();
|
||||
while(iterator.hasNext()){
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry entry = iterator.next();
|
||||
String key = String.valueOf(entry.getKey());
|
||||
String value = String.valueOf(entry.getValue());
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.github.binarywang.wxpay.util;
|
||||
|
||||
public class XmlConfig {
|
||||
|
||||
/**
|
||||
* 是否使用快速模式
|
||||
*
|
||||
* 如果设置为true,将会影响下面的方法,不再使用反射的方法来进行xml转换。
|
||||
* 1: BaseWxPayRequest的toXML方法
|
||||
* 2: BaseWxPayResult的fromXML方法
|
||||
* @see com.github.binarywang.wxpay.bean.request.BaseWxPayRequest#toXML
|
||||
* @see com.github.binarywang.wxpay.bean.result.BaseWxPayResult#fromXML
|
||||
*
|
||||
* 启用快速模式后,将能:
|
||||
* 1:性能提升约 10 ~ 15倍
|
||||
* 2:可以通过 graalvm 生成native image,大大减少系统开销(CPU,RAM),加快应用启动速度(亚秒级),加快系统部署速度(脱离JRE).
|
||||
*
|
||||
* 参考测试案例: com.github.binarywang.wxpay.bean.result.WxPayRedpackQueryResultTest#benchmark
|
||||
* 参考网址: https://www.graalvm.org/
|
||||
*/
|
||||
public static boolean fastMode = false;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user