新手开发SpringCloud中之如何调用Server端的方法(以HTTP的方式)
使用
import org.springframework.web.client.RestTemplate;
RestTemplate restTemplate;
这里有个小问题 如何使用注解
@Autowired的话
会报错
Field restTemplate in com.example.controller.ModulController required a bean of type ‘org.springframework.web.client.RestTemplate’ that could not be found.
所以只能自己自行注入创建Bean
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
url中的端口号记得是Server的端口号
@RequestMapping("/hi")
public String GetA(@RequestParam String name){
String url=“http://localhost:8761/Call”;
return restTemplate().getForObject(url, String.class);
}