1.设置配置类
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}
2.Controller调用
@Autowired
private RestTemplate restTemplate;
//方式一
MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
requestEntity.add("merchantOrderId", merchantOrderId);
String httpStatus = restTemplate.postForObject(merchantReturnUrl, requestEntity, String.class);
//方式二
MerchantOrdersVO merchantOrdersVO = orderVO.getMerchantOrdersVO();
merchantOrdersVO.setReturnUrl(payReturnUrl);
// 为了方便测试购买,所以所有的支付金额都统一改为1分钱
merchantOrdersVO.setAmount(1);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("imoocUserId","imooc");
headers.add("password","imooc");
HttpEntity<MerchantOrdersVO> entity =
new HttpEntity<>(merchantOrdersVO, headers);
ResponseEntity<IMOOCJSONResult> responseEntity =
restTemplate.postForEntity(paymentUrl,
entity,
IMOOCJSONResult.class);
IMOOCJSONResult paymentResult = responseEntity.getBody();