直接贴已完成代码
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直接去解析,后续会很难处理