当前位置:网站首页>Remember that resttemplate.getforentity failed to carry headers once, resttemplate exchange
Remember that resttemplate.getforentity failed to carry headers once, resttemplate exchange
2022-07-27 21:08:00 【Hua Weiyun】
Remember to use it once restTemplate.getForEntity carry header Solutions to failed requests . Because one day I suddenly received a demand , You need to access an interface , And change the interface , You must carry the current user's cookie. Because the system basically uses restTemplate.postForEntity, carry header No problem , So I changed it directly get, So it was a tragedy .
restTemplate.postForEntity The code is as follows
// obtain headerHttpHeaders headers = new HttpHeaders();HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();headers.add("Cookie", request.getHeader("Cookie"));// send out Map<String, Object> param = new HashMap<>();param.put("param", "param");HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(param), headers);ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class);String body = responseEntity.getBody();None of this is a problem , Requests are normal
So I put restTemplate.postForEntity Change it to restTemplate.getForEntity. An error message appears

After careful comparison and postForEntity The difference between .
I changed it to . Confident enough , Then it's a long way to find out why .
ResponseEntity<String> responseEntity = restTemplate.getForEntity("", String.class, entity);The final conclusion ,getforEntity No settings header Methods , Although I changed the order, I didn't report the wrong , But after the exchange , It's not setting at all header Value . There is no such thing as setHeader
restTemplate.exchage
Belongs to template method , Can directly set the request mode , Set request header . The key codes are as follows :
ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, new Object[0]);Using this method, you can get The request carries the request header .
other : I also found some usage on the Internet getForEntity, send out header The way to , Similar use filter, More trouble , I'm not going to expand it . Interested in their own Baidu .
边栏推荐
- 自动化测试----selenium(二)
- 中地数码:融合创新国产GIS 乘风而上助推实景三维中国建设
- Tips for file upload to bypass WAF
- 如何对话CIO/CTO
- Hcip day 5
- 北京/上海/广州/深圳DAMA-CDGA/CDGP数据治理认证报名条件
- 五大知名人士对于AI的忧虑
- How to talk to CIO / CTO
- Academic sharing | Tsinghua University, Kang Chongqing: power system carbon measurement technology and application (matlab code implementation)
- Obtain website shell permission based on file upload vulnerability
猜你喜欢

82.(cesium篇)cesium点在3d模型上运动

LeetCode每日一练 —— 链表中倒数第 k 个结点

Do you know about data synchronization?

Source Insight 4.0使用介绍

LeetCode-209-长度最小的子数组

PHP code audit 6 - file contains vulnerability

Academic sharing | Tsinghua University, Kang Chongqing: power system carbon measurement technology and application (matlab code implementation)

JS closure knowledge

IOU 目标跟踪其二:VIOU Tracker
![[Numpy] 数组索引和切片](/img/ce/34db7aef3fefe8a03e638d0838492f.png)
[Numpy] 数组索引和切片
随机推荐
SQL coding bug
Repeated DNA sequence [hash determination repetition + sliding window + bit operation of binary coding]
Elk too heavy? Try KFC log collection
Qt 链接MSSQL
自定义学习率
Automated testing - unittest framework
Brief description of tenant and multi tenant concepts in cloud management platform
Airiot Q & A issue 6 | how to use the secondary development engine?
How to realize document collaboration?
Hcip day 5
Hexagon_V65_Programmers_Reference_Manual(9)
Typroa 拼写检查: 缺少对于 中文 的字典文件
JS closure knowledge
Face recognition 5.1- insightface face face detection model training practice notes
LeetCode每日一练 —— 206. 反转链表
【历史上的今天】7 月 27 日:模型检测先驱出生;微软收购 QDOS;第一张激光照排的中文报纸
认识网络模型TCPIP模型
LeetCode-136-只出现一次的数字
NPDP|什么样的产品经理可以被称为优秀?
好开不贵,舒适安全!深度体验比亚迪宋Pro DM-i