SpringBoot使用RestTemplate 实现服务间的调用
1 启动被调用服务,获取被调用服务的 url 请求地址:
url:http://localhost:9001/course/direcrion?directionId=1&page=1&size=2;
2 在调用服务的启动类中追加 TestTemplate :
//使用RestTemplate这个组件实现服务间的调用
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
3 在该服务的Service实现类中 注入 RestTemplate :
//将RestTemplate注入进来
@Autowired
private RestTemplate restTemplate;
4 在该服务的Service实现类中使用注入的RestTemplate 调用被调用的服务
//通过调用course工程中的course/direction服务加载相关的推荐课程
//http://localhost:9001/course/direcrion?directionId=1&page=1&size=2
String url = "http://localhost:9001/course/direcrion?directionId=1&page=1&size=2";
//调用restTemplate中的方法(get/post等),url为上面的网址,.class为返