Springboot中如何通过RestTemplate实现简单的HTTP请求调用?
- 内容介绍
- 文章标签
- 相关推荐
本文共计388个文字,预计阅读时间需要2分钟。
前言:Spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接,我们只需传入url及返回值类型即可。相较于之前。
前言
spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与localhost:81/sample/GetData?msg={msg}&author={author}"; Map<String,String> param=new HashMap<>(); param.put("msg","Hello"); param.put("author","彦杰"); return restTemplateUtil.GetData(url,param); } @GetMapping("testPostJsonData") public String testPostJsonData(){ String url="localhost:81/sample/PostData"; JSONObject jsonObject=new JSONObject(); jsonObject.put("msg","hello"); jsonObject.put("author","彦杰"); return restTemplateUtil.PostJsonData(url,jsonObject); } @GetMapping("testPostFormData") public String testPostFormData(){ String url="localhost:81/sample/PostFormData"; MultiValueMap<String,String> param=new LinkedMultiValueMap<>(); param.add("msg","Hello"); param.add("author","彦杰"); return restTemplateUtil.PostFormData(url,param); } @GetMapping("GetData") public String getData(String msg, String author){ return msg+" "+author; } @PostMapping("PostData") public String postData(@RequestBody JSONObject jsonObject){ String msg=jsonObject.getString("msg"); String author=jsonObject.getString("author"); return msg+" "+author; } @PostMapping("PostFormData") public String PostFormData(String msg,String author){ return msg+" "+author; } @GetMapping("testGetData") public String testGetData(){ String url="localhost:81/sample/GetData?msg={msg}&author={author}"; Map<String,String> param=new HashMap<>(); param.put("msg","Hello"); param.put("author","彦杰"); return restTemplateUtil.GetData(url,param); } @GetMapping("testPostJsonData") public String testPostJsonData(){ String url="localhost:81/sample/PostData"; JSONObject jsonObject=new JSONObject(); jsonObject.put("msg","hello"); jsonObject.put("author","彦杰"); return restTemplateUtil.PostJsonData(url,jsonObject); } @GetMapping("testPostFormData") public String testPostFormData(){ String url="localhost:81/sample/PostFormData"; MultiValueMap<String,String> param=new LinkedMultiValueMap<>(); param.add("msg","Hello"); param.add("author","彦杰"); return restTemplateUtil.PostFormData(url,param); } @GetMapping("GetData") public String getData(String msg, String author){ return msg+" "+author; } @PostMapping("PostData") public String postData(@RequestBody JSONObject jsonObject){ String msg=jsonObject.getString("msg"); String author=jsonObject.getString("author"); return msg+" "+author; } @PostMapping("PostFormData") public String PostFormData(String msg,String author){ return msg+" "+author; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计388个文字,预计阅读时间需要2分钟。
前言:Spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接,我们只需传入url及返回值类型即可。相较于之前。
前言
spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与localhost:81/sample/GetData?msg={msg}&author={author}"; Map<String,String> param=new HashMap<>(); param.put("msg","Hello"); param.put("author","彦杰"); return restTemplateUtil.GetData(url,param); } @GetMapping("testPostJsonData") public String testPostJsonData(){ String url="localhost:81/sample/PostData"; JSONObject jsonObject=new JSONObject(); jsonObject.put("msg","hello"); jsonObject.put("author","彦杰"); return restTemplateUtil.PostJsonData(url,jsonObject); } @GetMapping("testPostFormData") public String testPostFormData(){ String url="localhost:81/sample/PostFormData"; MultiValueMap<String,String> param=new LinkedMultiValueMap<>(); param.add("msg","Hello"); param.add("author","彦杰"); return restTemplateUtil.PostFormData(url,param); } @GetMapping("GetData") public String getData(String msg, String author){ return msg+" "+author; } @PostMapping("PostData") public String postData(@RequestBody JSONObject jsonObject){ String msg=jsonObject.getString("msg"); String author=jsonObject.getString("author"); return msg+" "+author; } @PostMapping("PostFormData") public String PostFormData(String msg,String author){ return msg+" "+author; } @GetMapping("testGetData") public String testGetData(){ String url="localhost:81/sample/GetData?msg={msg}&author={author}"; Map<String,String> param=new HashMap<>(); param.put("msg","Hello"); param.put("author","彦杰"); return restTemplateUtil.GetData(url,param); } @GetMapping("testPostJsonData") public String testPostJsonData(){ String url="localhost:81/sample/PostData"; JSONObject jsonObject=new JSONObject(); jsonObject.put("msg","hello"); jsonObject.put("author","彦杰"); return restTemplateUtil.PostJsonData(url,jsonObject); } @GetMapping("testPostFormData") public String testPostFormData(){ String url="localhost:81/sample/PostFormData"; MultiValueMap<String,String> param=new LinkedMultiValueMap<>(); param.add("msg","Hello"); param.add("author","彦杰"); return restTemplateUtil.PostFormData(url,param); } @GetMapping("GetData") public String getData(String msg, String author){ return msg+" "+author; } @PostMapping("PostData") public String postData(@RequestBody JSONObject jsonObject){ String msg=jsonObject.getString("msg"); String author=jsonObject.getString("author"); return msg+" "+author; } @PostMapping("PostFormData") public String PostFormData(String msg,String author){ return msg+" "+author; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

