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.youkuaiyun.com/youbitch1/article/details/106117173
本文介绍如何在SpringBoot项目中使用RestTemplate工具类发送HTTP请求,通过示例代码展示了getForObject方法的使用,同时提供了请求端的响应代码。
4548

被折叠的 条评论
为什么被折叠?



