SpringBoot给我们提供了一个发送Http请求的工具类RestTemplate,使用这个工具类可以很方便的写出http请求代码。
示例代码:
RestTemplate restTemplate = new RestTemplate();
String forObject = restTemplate.getForObject("http://localhost:9096/id.do",String.class);
System.out.println(forObject); //输出1
请求端http://localhost:9096/id.do代码:
@ResponseBody
@RequestMapping("/id.do") //这里为空或者是/都能进入该方法
public String UserId() {
return "1";
}
详细:https://blog.csdn.net/youbitch1/article/details/106117173