首页 > 编程 > Java > 正文

SpringMvc微信支付回调示例代码

2019-11-26 13:45:59
字体:
来源:转载
供稿:网友

介绍

大家都知道微信支付的回调链接要求不能跟参数,但又要接收返回的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;}

总结

以上就是这篇文章的全部内容了,希望能对大家的学习或者工作带来一定的帮助,如果有疑问大家可以留言交流。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表