RestTemplate 统一添加 Header
一、添加拦截器
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;}@Overridepublic 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 {@Beanpublic RestTemplate restTemplate() {List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();interceptors.add(new HeaderRequestInterceptor("token", "123"));RestTemplate restTemplate = new RestTemplate();restTemplate.setInterceptors(interceptors);return restTemplate;}
}
三、使用
 @Autowiredprivate RestTemplate restTemplate;

Spring boot RestTemplate 统一添加 Header相关推荐

  1. 注解参数获取不到_scm-springboot基于spring boot的统一注解缓存

    scm-springboot 基于spring boot的统一注解缓存,支持mencached.redis.ehcache的缓存无缝切换.支持单个缓存设置过期时间,灵活的key设置规则,采用fastj ...

  2. 基于spring boot的统一异常处理

    基于spring boot的统一异常处理 参考文章: (1)基于spring boot的统一异常处理 (2)https://www.cnblogs.com/knyel/p/7804237.html 备 ...

  3. Spring boot异常统一处理方法:@ControllerAdvice注解的使用、全局异常捕获、自定义异常捕获

    Spring boot异常统一处理方法:@ControllerAdvice注解的使用.全局异常捕获.自定义异常捕获 参考文章: (1)Spring boot异常统一处理方法:@ControllerAd ...

  4. 基于Spring Boot 的统一错误处理

    我将展示一下如何在Spring Boot 下实现统一错误处理,使用的工具是IDEA. 首先,我们需要定义一个自定义名称的错误控制类,我把它叫做WebErrorController,这个类需要实现Err ...

  5. Spring Boot学习——统一异常处理

    Spring Boot学习--统一异常处理 参考文章: (1)Spring Boot学习--统一异常处理 (2)https://www.cnblogs.com/aston/p/7258834.html ...

  6. Spring Boot配置统一格式Result返回值(一)

    在Web开发中,前后端分离开发已经深入人心,这样的开发模式对于开发优雅美观的web应用非常有益.我在网上查阅了一些资料,学习了在Spring Boot开发中使用有利于前端小伙伴合作开发的统一格式返回值 ...

  7. Spring Boot 接口统一前缀

    需求 需求如题,想给一个 spring boot 项目的所有请求路径添加统一前缀,可以通过 context-path 来配置.但是在同时存在静态资源和 Controller 接口的项目中,如果希望静态 ...

  8. 无返回值_只需一步,在Spring Boot中统一Restful API返回值格式与处理异常

    统一返回值 在前后端分离大行其道的今天,有一个统一的返回值格式不仅能使我们的接口看起来更漂亮,而且还可以使前端可以统一处理很多东西,避免很多问题的产生. 比较通用的返回值格式如下: public cl ...

  9. Spring Boot RestTemplate 忽略证书访问https

    在Spring Boot中,RestTemplate 一般使用 @Autowired 注解自动装配, 类似: @Autowiredprivate RestTemplate restTemplate; ...

最新文章

  1. Scrum Master的职业发展路线
  2. 利用Git Bash 远程访问服务器
  3. python单链表操作_单链表的创建、增删改查等操作(Python实现)
  4. delphi和 java通过socket通信的中文问题
  5. 基于HTML5 WebGL实现 json工控风机叶轮旋转
  6. Win2003 + SQL 2005 做数据库集群总结(虚拟机)
  7. mfc让图片与按钮一起_对许多张图片进行批量裁剪,看看我是如何快速做到的
  8. oracle11g安装
  9. 基于JAVA+SpringMVC+Mybatis+MYSQL的在线论坛管理系统
  10. 数据库连接池的深入理解
  11. 全链路压测原理篇(方案 概念 架构 实现)
  12. AS更换背景主题以及背景图片
  13. 讲给后台程序员看的前端系列教程(17)——文本样式
  14. Cocos技术派 | 3d人物渲染详细教程
  15. 什么是抽象类,什么情况下会用到抽象类?
  16. jsptitle换行_fullcalendar中title内容过长显示不全的问题
  17. [OHIF-Viewers]医疗数字阅片-医学影像-cornerstone-core-Cornerstone.js提供了一个完整的基于Web的医学成像平台。...
  18. python基础教学习题_python基础|数据类型练习题
  19. python:pyinstaller打包可执行文件
  20. Java基础:Java语言简介

热门文章

  1. 前端vue面试总结,常见的,没有更全,只有更细,更新中
  2. JS 简单实现电子时钟
  3. 小刀娱乐网:引流的四个思维分析,如何更好发展网站!
  4. Android 输入非中文密码
  5. 基于UDS的BootLoader设计——架构设计及规范
  6. 医药采购之采购单明细添加查询
  7. 计算机待机快捷键,待机快捷键
  8. 关于UFW防火墙应用小结
  9. arduino程序烧录(USBasp下载器)
  10. linux qt5.9/5.14 “This program was compiled against version 2.6.1 of the Protocol“