当前位置: 首页 > news >正文

springboot发送request请求的方式

前言

java发送请求的方法有很多,这里只介绍两种。

hutool和RestTemplate

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

hutool方式

get请求

	@GetMapping("/userEleList")@ResponseBodypublic JSONObject userEleList(@RequestParam(name = "userCode") String userCode, HttpServletRequest request) {String Authorization = request.getHeader("Authorization");String token = request.getHeader("token");String body = HttpUtil.createGet("http://ip:8068/userEleList?userCode=" + userCode).header("Authorization", Authorization).header("token", token).execute().body();return JSONObject.parseObject(body);}
	@GetMapping("/getKdToken")@ResponseBodypublic JSONObject userEleList(@RequestParam(name = "appId") String appId,@RequestParam(name = "appSecret") String appSecret,@RequestParam(name = "grantType") String grantType) {String post = HttpUtil.get("http://ip:8068/getKdToken?appId=" + appId + "&appSecret=" + appSecret + "&grantType=" + grantType);return JSONObject.parseObject(post);}

post请求

	@PostMapping("/eleRechargeMoneyAllList")@ResponseBodypublic JSONObject eleRechargeMoneyAllList(@RequestBody Map<String, Object> map, HttpServletRequest request) {String Authorization = request.getHeader("Authorization");String token = request.getHeader("token");Object elemeterId = map.get("elemeterId");Object money = map.get("money");Object selOrderno = map.get("selOrderno");String post = HttpUtil.createPost("http://ip:8068/eleRechargeMoneyAllList?elemeterId=" + elemeterId + "&money=" + money + "&adds=0&selOrderno=" + selOrderno + "&payType=40").header("Authorization", Authorization).header("token", token).execute().body();return JSONObject.parseObject(post);}
	@PostMapping("/GetClientByCnumber")@ResponseBodypublic JSONObject GetClientByCnumber(@RequestBody Map<String, Object> map) {String post = HttpUtil.post("http://ip:8006/GetClientByCnumber", map);return JSONObject.parseObject(post);}

RestTemplate方式

	@PostMapping("/userPricePay")@ResponseBodypublic JSONObject userPricePay(@RequestBody Map<String, Object> map, HttpServletRequest request) {String sign = request.getHeader("sign");RestTemplate restTemplate = new RestTemplate();// 设置请求头,指定Content-Type为application/jsonHttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);//        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);headers.set("sign", sign);// 将解析后的 JSON 对象转换为 MultiValueMapHttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(map, headers);ResponseEntity<String> exchange = restTemplate.exchange("https://ip:8080/userPricePay", HttpMethod.POST, requestEntity, String.class);return JSONObject.parseObject(exchange.getBody());}

http://www.mrgr.cn/news/20134.html

相关文章:

  • npm login 或者 npm publish 超时timeout
  • 不要为《黑神话:悟空》买新电脑,ToDesk云电脑体验分享
  • 动态规划---一和零
  • “解决MySQL容器启动后无法编辑文件的问题:实用指南“
  • 火山引擎VeDI赋能小城酒店业,助力“流量”向“留量”转化
  • 一种基于YOLOv10的高精度工业油污缺陷检测算法(原创自研)
  • kali——msfconsole的使用
  • 【计算机视觉前沿研究 热点 顶会】ECCV 2024中扩散模型有关的论文
  • 【Qt】Qt和JavaScript使用QWebChannel交互
  • 等保测评中的数据安全风险评估:企业实战
  • Linux:深入剖析计算机软硬件架构
  • 阿贝云免费虚拟主机和免费云服务器评测
  • 本地电脑交叉编译ffmpeg 到 windows on arm64
  • go程序解说
  • 没有35类可以做特许经营加盟不!
  • 字节内部培训的《大模型落地应用案例集》,52个大模型落地精选案例!
  • 前端登录鉴权——以若依Ruoyi前后端分离项目为例解读
  • 全球主要论文知识库-学习资源
  • 比特币客户端和API
  • 体会循环---冒泡排序