一、在启动类配置RestTemplate
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
二、引入json的依赖(获取response body可以用到)
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
三、依赖注入
@Autowired
private RestTemplate restTemplate;
@Autowired
private ObjectMapper objectMapper;
四、调接口
String url = "";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("honeycombToken", "");
HashMap<String, String> map = new HashMap<>();
map.put("", "");
map.put("", "");
HttpEntity<HashMap<String, String>> hashMapHttpEntity = new HttpEntity<>(map,httpHeaders);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, hashMapHttpEntity, String.class);
JsonNode jsonNode = objectMapper.readTree(response.getBody());
String token = jsonNode.get("data").get("token").toString();