//~~~~~~~~~~~~~~~~~~~~ 设置请求头信息 ~~~~~~~~~~~~~~~~~~~~
HttpHeaders headers = new HttpHeaders();
headers.add("appid", "xxxxxxxxxx");
headers.add("timestamp", "xxxxxxxxxx");
headers.add("sign", "xxxxxxxxxx");
headers.add("apisign", "xxxxxxxxxx");
//设置编码格式为UTF-8
MediaType type = MediaType.parseMediaType("application/json; charset = UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
//~~~~~~~~~~~~~~~~~~~~ 封装请求头和请求体 ~~~~~~~~~~~~~~~~~~~~
HttpEntity<String> httpEntity = new HttpEntity<>("params(业务参数Json字符串)", headers);
//~~~~~~~~~~~~~~~~~~~~ 调用接口,接收响应 ~~~~~~~~~~~~~~~~~~~~
ResponseEntity<String> responseEntity = restTemplate.postForEntity("url", httpEntity, String.class);
logger.info(">>>>> 企业下查询发票响应数据,信息: -> {}", responseEntity.getBody());
// 判断请求是否成功
if (responseEntity.getStatusCodeValue() != HttpStatus.OK.value()) {
logger.error("请求接口失败,信息: -> {}", responseEntity.getBody());
}
标签:请求,RestTemplate,MediaType,headers,add,添加,responseEntity,xxxxxxxxxx
From: https://www.cnblogs.com/Anhk/p/17137944.html