软件编程
位置:首页>> 软件编程>> java编程>> SpringMvc微信支付回调示例代码

SpringMvc微信支付回调示例代码

作者:daisy  发布时间:2023-01-24 08:00:03 

标签:springmvc,微信,支付,回调

介绍

大家都知道微信支付的回调链接要求不能跟参数,但又要接收返回的xml数据。我开始使用@RequestBody注解在参数上,希望能获取xml数据,测试失败。最后使用HttpServletRequest去获取数据成功了。

示例代码


@RequestMapping("/weixinpay/callback")
public String callBack(HttpServletRequest request){
InputStream is = request.getInputStream();
String xml = StreamUtil.inputStream2String(is, "UTF-8")
/**
* 后面把xml转成Map根据数据作逻辑处理
*/
}

/**
* InputStream流转换成String字符串
* @param inStream InputStream流
* @param encoding 编码格式
* @return String字符串
*/
public static String inputStream2String(InputStream inStream, String encoding){
String result = null;
try {
if(inStream != null){
 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
 byte[] tempBytes = new byte[_buffer_size];
 int count = -1;
 while((count = inStream.read(tempBytes, 0, _buffer_size)) != -1){
   outStream.write(tempBytes, 0, count);
 }
 tempBytes = null;
 outStream.flush();
 result = new String(outStream.toByteArray(), encoding);
}
} catch (Exception e) {
result = null;
}
return result;
}

总结

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com