基于浏览器发送请求url获取数据的思路一样,也通过发送url的方式远程获取我们想要的数据
实现步骤
1.注册RestTemplate
在启动类上加入以下代码,将RestTemplate交给spring容器管理
@Bean public RestTemplate restTemplate(){ return new RestTemplate(); }
2.然后就可以利用它发送http请求了
然后在实现类这里注入RestTemplate
@Autowired private RestTemplate restTemplate;
然后再用RestTemplate提供的api,根据自己所需要的请求方式合理选择
例如:你是post的方法你就选择restTemplate.postForObject(),这里面有两个参数,第一个参数写你的url地址(即你要发送的请求)。第二个参数写你想要的返回类型
这样即可完成微服务的远程调用