第一步,在springboot的启动类(Application)中添加RestTemplate的类
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
第二步,在controller层或service层使用restTemplate发送请求
@Autowired
private RestTemplate restTemplate;
public void httpGet(){
// url 自行填写
String url = 'https://www.***.com';
// 发送get请求,并用String数据格式接收
String result = restTemplate.getForObject(url, String.class);
// 获取结果转 json
System.out.println(result);
JSONObject jsonObject = JSON.parseObject(result);
// 转为json后,则可以根据json的键值取出value,
// jsonObject..get()中填写键值(key)
String value = (String) jsonObject.get("key");
}