@Autowired
private RestTemplate restTemplate;
1、post或get请求
try {
//添加请求头
HttpHeaders headers = new HttpHeaders();
headers.add("key", "value");
//T需要传递的body参数内容的类
HttpEntity<T> requestEntity = new HttpEntity<>(t, headers);
//get请求把postForEntity修改为getForEntity,也可以按照以下方式请求
//ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, T.class);
//HttpStatus status = responseEntity.getStatusCode();
ResponseEntity<T> responseEntity = restTemplate.postForEntity(URL,requestEntity, T.class);
if (responseEntity.getStatusCode().is2xxSuccessful()) {
logger.info("请求成功");
}
} catch (Exception e) {
logger.error("请求失败:", e);
}