SpringMvc微信支付回调示例代码_Java教程-查字典教程网
SpringMvc微信支付回调示例代码
SpringMvc微信支付回调示例代码
发布时间:2016-12-28 来源:查字典编辑
摘要:介绍大家都知道微信支付的回调链接要求不能跟参数,但又要接收返回的xml数据。我开始使用@RequestBody注解在参数上,希望能获取xml...

介绍

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

总结

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

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Java学习
    热门Java学习
    编程开发子分类