java - 覆盖应用于 RestTemplate 的 RestTemplateCustomizer
问题描述
我正在为我的一个应用程序RestTemplateCustomizer
配置通用配置RestTemplate
。但是现在在我的一个客户端类中,我需要用另一个自定义拦截器覆盖我拥有的一个自定义拦截器。
我可以在RestTemplate
不使用的情况下获得实例,RestTemplateBuilder
并在此处添加我的所有配置,这应该可以工作,但我真的不想要这种方法。
我想解决这个问题的方法是将所有拦截器附加到RestTemplate
,找到我的自定义拦截器 1,将其删除并添加新的自定义拦截器 2。
public MyRestClient( RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
for(int i=0 ;i<this.restTemplate.getInterceptors().size();i++) {
if(this.restTemplate.getInterceptors().get(i).getClass().getSimpleName().equalsIgnoreCase("MyCustomInterceptor1")){
this.restTemplate.getInterceptors().remove(i);
}
}
this.restTemplate.getInterceptors().add(new MyCustomInterceptor2());
}
它似乎工作正常,但有没有更好的方法来做到这一点?
解决方案
推荐阅读
- javascript - 无重复的随机图像
- alpine - alpinelinux 3.12 缺少 man 包?
- javascript - 当我部署 - 加载资源失败:服务器响应状态为 404 ()
- php - 整数未插入 MySQL 数据库错误
- javascript - 节点 JS 自动重新加载
- c++ - DirectX 11 如何在 GPU 上处理来自 sharedHandle 的图像
- javascript - 在 React JSX 中的映射数组内映射嵌套数组
- gtk3 - 在 vala 中管理 GTK .ui 文件中的信号
- python - 绑定鼠标滚轮 Tkinter
- c++ - 指向基类的唯一指针