springboot发送request请求的方式

打印 上一主题 下一主题

主题 556|帖子 556|积分 1668

媒介

java发送请求的方法有很多,这里只先容两种。
hutool和RestTemplate

下边提供两种后端发送请求的方式,一个是基于hutool工具的,一个是基于RestTemplate的,为什么要写这两种呢,由于有的时间用hutool的方式不太管用,有的时间用RestTemplate也不太管用,所以就且换着用,谁能用,用谁。
hutool方式

get请求

  1.         @GetMapping("/userEleList")
  2.     @ResponseBody
  3.     public JSONObject userEleList(@RequestParam(name = "userCode") String userCode, HttpServletRequest request) {
  4.         String Authorization = request.getHeader("Authorization");
  5.         String token = request.getHeader("token");
  6.         String body = HttpUtil.createGet("http://ip:8068/userEleList?userCode=" + userCode)
  7.                 .header("Authorization", Authorization)
  8.                 .header("token", token)
  9.                 .execute()
  10.                 .body();
  11.         return JSONObject.parseObject(body);
  12.     }
复制代码
  1.         @GetMapping("/getKdToken")
  2.     @ResponseBody
  3.     public JSONObject userEleList(@RequestParam(name = "appId") String appId,
  4.                                   @RequestParam(name = "appSecret") String appSecret,
  5.                                   @RequestParam(name = "grantType") String grantType) {
  6.         String post = HttpUtil.get("http://ip:8068/getKdToken?appId=" + appId + "&appSecret=" + appSecret + "&grantType=" + grantType);
  7.         return JSONObject.parseObject(post);
  8.     }
复制代码
post请求

  1.         @PostMapping("/eleRechargeMoneyAllList")
  2.     @ResponseBody
  3.     public JSONObject eleRechargeMoneyAllList(@RequestBody Map<String, Object> map, HttpServletRequest request) {
  4.         String Authorization = request.getHeader("Authorization");
  5.         String token = request.getHeader("token");
  6.         Object elemeterId = map.get("elemeterId");
  7.         Object money = map.get("money");
  8.         Object selOrderno = map.get("selOrderno");
  9.         String post = HttpUtil
  10.                 .createPost("http://ip:8068/eleRechargeMoneyAllList?elemeterId=" + elemeterId + "&money=" + money + "&adds=0&selOrderno=" + selOrderno + "&payType=40")
  11.                 .header("Authorization", Authorization)
  12.                 .header("token", token)
  13.                 .execute()
  14.                 .body();
  15.         return JSONObject.parseObject(post);
  16.     }
复制代码
  1.         @PostMapping("/GetClientByCnumber")
  2.         @ResponseBody
  3.          public JSONObject GetClientByCnumber(@RequestBody Map<String, Object> map) {
  4.              String post = HttpUtil.post("http://ip:8006/GetClientByCnumber", map);
  5.              return JSONObject.parseObject(post);
  6.          }
复制代码
RestTemplate方式

  1.         @PostMapping("/userPricePay")
  2.         @ResponseBody
  3.         public JSONObject userPricePay(@RequestBody Map<String, Object> map, HttpServletRequest request) {
  4.                    String sign = request.getHeader("sign");
  5.                    RestTemplate restTemplate = new RestTemplate();
  6.                    // 设置请求头,指定Content-Type为application/json
  7.                    HttpHeaders headers = new HttpHeaders();
  8.                    headers.setContentType(MediaType.APPLICATION_JSON);
  9.                 //        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
  10.                    headers.set("sign", sign);
  11.                    // 将解析后的 JSON 对象转换为 MultiValueMap
  12.                    HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(map, headers);
  13.                    ResponseEntity<String> exchange = restTemplate.exchange("https://ip:8080/userPricePay", HttpMethod.POST, requestEntity, String.class);
  14.                    return JSONObject.parseObject(exchange.getBody());
  15.         }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

种地

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表