惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
小众软件
小众软件
V
V2EX
博客园 - Franky
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
量子位
博客园 - 【当耐特】
雷峰网
雷峰网
WordPress大学
WordPress大学
Jina AI
Jina AI
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
V
Visual Studio Blog
Microsoft Security Blog
Microsoft Security Blog

博客园 - ㄓㄤㄑㄧㄤ

sdkman同时存在多个jdk的方式 com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed at Sat Apr 20 09:55:48 CST 2024 $elemMatch使用 python部署在后台执行 SpringBoot获取当前运行环境三种方式 nginx流量复制将请求同时发送到正式和测试环境 一个注解优雅的实现 接口数据脱敏 centOS7 开放/移除指定IP访问指定端口 mac 系统升级到12.4后 mvn 命令 报错:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? mac Zip 常用命令 Mac下php服务部署 更新setuptools和pip时报错Command "python setup.py egg_info" failed with error code 1 in 解决 CentOS 7 执行 firewall-cmd 防火墙命令遇到的 ModuleNotFoundError: No module named ‘gi’ 最新Centos7安装python3并与python2共存 springcloud2.0以上版本_eureka控制台显示_找不到${spring.cloud.client.ipAddress}_没有显示成IP地址 删除centos7中自带有python2.7 Git 常用命令,持续更新 python操作mongodb查询出现Object of type 'ObjectId' is not JSON serializable解决方法 sqlserver按年、季度、月份、周统计订单销量
RestTemplate 统一添加 Header
ㄓㄤㄑㄧㄤ · 2022-01-20 · via 博客园 - ㄓㄤㄑㄧㄤ

一、添加拦截器

public class HeaderRequestInterceptor implements ClientHttpRequestInterceptor {

    private final String headerName;

    private final String headerValue;

    public HeaderRequestInterceptor(String headerName, String headerValue) {
        this.headerName = headerName;
        this.headerValue = headerValue;
    }

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        request.getHeaders().set(headerName, headerValue);
        return execution.execute(request, body);
    }
}

二、RestTemplate Bean

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate() {
        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
        interceptors.add(new HeaderRequestInterceptor("token", "123"));

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setInterceptors(interceptors);

        return restTemplate;
    }
}

三、使用

@Autowired private RestTemplate restTemplate;
原文链接:https://www.cnblogs.com/victorbu/p/12708340.html