一、獲取請求頭
接口示例1:
@PostMapping(value = "/wxPayNotify")
public R<String> wxPayNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
weChatPaymentService.wxPayNotify(request, response);
return R.ok();
}
1、從 request?對象中獲取請求頭:
//語法:request.getHeader(請求頭key)
String nonce = request.getHeader(wxPayConstant.getHeader_nonce());
String signature = request.getHeader(wxPayConstant.getHeader_signature());
二、獲取請求體
1、從 request?對象中,使用緩沖流讀取器、stream流等方式獲取請求體文章來源:http://www.zghlxwxcb.cn/news/detail-592968.html
推薦寫法一:文章來源地址http://www.zghlxwxcb.cn/news/detail-592968.html
//寫法一(推薦此寫法):使用讀取器配合java stream流,獲取接口請求體
String requestBody = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
或者
String requestBody = request.getReader()
到了這里,關(guān)于Java Restful API接口獲取請求頭、請求體、以及設(shè)置響應(yīng)狀態(tài)碼、應(yīng)答(響應(yīng))體等的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!