当前位置:网站首页>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 .
边栏推荐
- Understanding network model TCPIP model
- IOU 目标跟踪其一:IOU Tracker
- Hexagon_V65_Programmers_Reference_Manual(7)
- Automatic test solution based on ATX
- 14 day Hongmeng device development practice - Chapter 7 device networking cloud learning notes
- 知识管理系统推动企业信息化发展
- Leetcode-209- subarray with the smallest length
- Hexagon_ V65_ Programmers_ Reference_ Manual(5)
- 坚持做一件事情
- [program life] "stage summary" - unwilling to be ordinary
猜你喜欢

The variable "lattice" or class "lattice.latticeeasy" (matlab) is not defined

如何让个性化推荐即刻触达?云原生数据库GaussDB(for Redis)来助力

Go --- automatic recompilation of air

Hexagon_V65_Programmers_Reference_Manual(5)

【历史上的今天】7 月 27 日:模型检测先驱出生;微软收购 QDOS;第一张激光照排的中文报纸
![[dart] a programming language for cross end development](/img/e1/1167a322bb9f276f2e00fb12414d17.png)
[dart] a programming language for cross end development

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

Leetcode-209- subarray with the smallest length

MapGIS三维场景渲染技术与应用

二舅,为什么火了?
随机推荐
A lock faster than read-write lock. Don't get to know it quickly
Hexagon_ V65_ Programmers_ Reference_ Manual(7)
坚持做一件事情
Source Insight 4.0使用介绍
IPv4/IPv6、DHCP、网关、路由
Hexagon_ V65_ Programmers_ Reference_ Manual(8)
R语言使用t.test函数执行t检验验证总体均值是否是某个特定的值(从样本集推论总体均值)
[today in history] July 27: model testing pioneer was born; Microsoft acquires qdos; The first laser typesetting Chinese newspaper
[program life] "stage summary" - unwilling to be ordinary
R语言使用epiDisplay包的power.for.2p函数进行效用分析 ( 效能分析、Power analysis)、给定两个样本的比例值(proportions)、样本量计算效用值
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用deviance函数计算出模型的残差平方和
Qt opengl 让物体在关照下动起来,形成动画
Beijing / Shanghai / Guangzhou / Shenzhen dama-cdga/cdgp data governance certification registration conditions
Knowledge management system promotes the development of enterprise informatization
Go --- automatic recompilation of air
如何查看蓝牙耳机的蓝牙版本
Face recognition 5.1- insightface face face detection model training practice notes
[numpy] broadcast mechanism
Codeforces 1706e merge + heuristic merge + st table
Where is the program?