springboot 發起對外http post請求


直接貼已完成代碼

public PayResponse payHttpRequest( MultiValueMap<String, String> params, String signature){
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
HttpMethod method = HttpMethod.POST;
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
params.add("appKey",appKey);
params.add("secret",signature);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, httpHeaders);
//執行HTTP請求
try{
ResponseEntity<PayResponse> response = restTemplate.postForEntity(orderCreateUrl, requestEntity, PayResponse.class);
return response.getBody();
}catch (Exception e){
return null;
}
}
1.HttpEntity最好使用MultiValueMap進行發起,不然容易出錯
2.restTemplate.postForEntity(orderCreateUrl, requestEntity, PayResponse.class) 中的元類,會在得到response響應后根據對應的類進行數據解析,不要用Object直接去解析,后續會很難處理


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM