SpringMvc微信支付回調(diào)示例代碼
介紹
大家都知道微信支付的回調(diào)鏈接要求不能跟參數(shù),但又要接收返回的xml數(shù)據(jù)。我開始使用@RequestBody注解在參數(shù)上,希望能獲取xml數(shù)據(jù),測試失敗。最后使用HttpServletRequest去獲取數(shù)據(jù)成功了。
示例代碼
@RequestMapping("/weixinpay/callback")
public String callBack(HttpServletRequest request){
InputStream is = request.getInputStream();
String xml = StreamUtil.inputStream2String(is, "UTF-8")
/**
* 后面把xml轉(zhuǎn)成Map根據(jù)數(shù)據(jù)作邏輯處理
*/
}
/**
* InputStream流轉(zhuǎn)換成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;
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望能對大家的學(xué)習(xí)或者工作帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
在SpringBoot中如何利用Redis實現(xiàn)互斥鎖
當(dāng)我們利用Redis存儲熱點數(shù)據(jù)時,突然就過期失效或者被刪除了,導(dǎo)致大量請求同時訪問數(shù)據(jù)庫,增加了數(shù)據(jù)庫的負載,為減輕數(shù)據(jù)庫的負載我們利用互斥鎖,本文重點介紹在SpringBoot中如何利用Redis實現(xiàn)互斥鎖,感興趣的朋友一起看看吧2023-09-09
mybatis-plus自帶QueryWrapper自定義sql實現(xiàn)復(fù)雜查詢實例詳解
MyBatis-Plus是一個MyBatis(opens new window)的增強工具,在 MyBatis的基礎(chǔ)上只做增強不做改變,MyBatis可以無損升級為MyBatis-Plus,這篇文章主要給大家介紹了關(guān)于mybatis-plus自帶QueryWrapper自定義sql實現(xiàn)復(fù)雜查詢的相關(guān)資料,需要的朋友可以參考下2022-10-10
解析SpringBoot中@Autowire注解的實現(xiàn)原理
在開發(fā)Java項目時,依賴注入是一種常見的實現(xiàn)方式,SpringBoot框架通過@Autowired注解來實現(xiàn)依賴注入的功能,本文將介紹SpringBoot中 Autowired注解實現(xiàn)的原理2023-06-06

