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

推荐订阅源

人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
月光博客
月光博客
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
小众软件
小众软件
量子位
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog
U
Unit 42
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - ㄓㄤㄑㄧㄤ

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