介紹
大家都知道微信支付的回調鏈接要求不能跟參數,但又要接收返回的xml數據。我開始使用@RequestBody
注解在參數上,希望能獲取xml數據,測試失敗。最后使用HttpServletRequest
去獲取數據成功了。
示例代碼
1
2
3
4
5
6
7
8
|
@RequestMapping ( "/weixinpay/callback" ) public String callBack(HttpServletRequest request){ InputStream is = request.getInputStream(); String xml = StreamUtil.inputStream2String(is, "UTF-8" ) /** * 后面把xml轉成Map根據數據作邏輯處理 */ } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
/** * 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; } |
總結
以上就是這篇文章SpringMvc微信支付回調示例代碼的全部內容了,希望能對大家的學習或者工作帶來一定的幫助,如果有疑問大家可以留言交流。