当前位置:网站首页>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 .
边栏推荐
- Ipv4/ipv6, DHCP, gateway, routing
- What are the application scenarios of real name authentication in the cultural tourism industry?
- How to calculate the execution time in the function resource usage when using the timer trigger type to process database data?
- QT OpenGL makes objects move under care to form animation
- 认识网络模型网络模型概述
- Obtain website shell permission based on file upload vulnerability
- mcu日志输出的一种方法
- [Numpy] 数组索引和切片
- NPDP|什么样的产品经理可以被称为优秀?
- Opencv implements image clipping and scaling
猜你喜欢

最新版web漏洞扫描工具AppScan\AWVS\Xray安装及使用教程

Recommend a powerful search tool listary

中地数码:融合创新国产GIS 乘风而上助推实景三维中国建设
![[Numpy] 数组索引和切片](/img/ce/34db7aef3fefe8a03e638d0838492f.png)
[Numpy] 数组索引和切片

Automated testing ----- selenium (II)

Do you know about data synchronization?

Face recognition 5.1- insightface face face detection model training practice notes

Go --- automatic recompilation of air

API Gateway介绍

Installation and use tutorial of the latest version of Web vulnerability scanning tool appscan\awvs\xray
随机推荐
PHP code audit 6 - file contains vulnerability
Hexagon_ V65_ Programmers_ Reference_ Manual(9)
Beijing / Shanghai / Guangzhou / Shenzhen dama-cdga/cdgp data governance certification registration conditions
How to calculate the execution time in the function resource usage when using the timer trigger type to process database data?
Things about stack migration
Qt OPenGL 光的漫反射
Arduino development (II)_ RGB light control method based on Arduino uno development board
Natapp intranet penetration tool Internet access personal projects
Codeforces 1706e merge + heuristic merge + st table
IPv4/IPv6、DHCP、网关、路由
如何对话CIO/CTO
PG 之 Free Space Map & Visibility Map
飞信卒于2022:中国移动一手好牌被打烂,5亿用户成“僵尸”
[deep learning] pytoch torch Autograd automatic differential engine
SLIM:自监督点云场景流与运动估计(ICCV 2021)
五大知名人士对于AI的忧虑
Know the transmission medium, the medium of network communication
Overview of understanding the physical layer of transmission media
认识网络模型TCPIP模型
How to talk to CIO / CTO