首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过获取已经创建的RestTemplate来拦截RestTemplate

通过获取已经创建的RestTemplate来拦截RestTemplate
EN

Stack Overflow用户
提问于 2018-02-05 14:55:19
回答 1查看 1.1K关注 0票数 0

我有拦截RestTemplate的逻辑,我在配置文件(SecurityConfiguration.java)中添加/注册这个RestTemplate,但是我想通过获取已经注册的RestTemplate对象从另一个配置文件中添加拦截器:

代码语言:javascript
复制
public class TranslogRestTemplateCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        boolean isRestTemplate = false;
        try {
            if (context.getBeanFactory() != null) {
                isRestTemplate = (context.getBeanFactory().getBean(RestTemplate.class) != null);
            }
        } catch (BeansException e) {
            return false;
        }
        return isRestTemplate;
    }
}

配置类:

代码语言:javascript
复制
@Configuration
public class RestTemplateConfig {

    private final MyInterceptor myInterceptor;

    @Value("${com.pqr.you.rest.enabled:true}")
    private boolean transEnabled;

    @Autowired
    public RestTemplateConfig(MyInterceptor myInterceptor) {
        this.myInterceptor = myInterceptor;
    }

    @Autowired
    private ApplicationContext appContext;

    // The logic added below is not working for me
    @Bean
    @Conditional(TranslogRestTemplateCondition.class)
    public RestTemplate addInterceptor(ApplicationContext appContext) {
        RestTemplate restTemplate = appContext.getBean(RestTemplate.class);
        if (transEnabled) {
            List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
            interceptors.add(myInterceptor);
            restTemplate.setInterceptors(interceptors);
        }
        return restTemplate;
    }
}

RestTemplate的实际逻辑(它将与所需的拦截器一起返回)和一些其他值(在返回此restTemplate时,我的拦截器也需要在这里添加,而不超过现有值)或,方法是接受下面的restTempalte对象并将MyInterceptor添加到restTemplate.中。

代码语言:javascript
复制
@Configuration
public class SecurityConfiguration {

    @Bean
    public AbcInterceptor abcRequestInterceptor(XyzService xyzService) {
        return new AbcInterceptor("abc-app", null, xyzService);
    }

    // I dont want to create bean here 
    /*@Bean
    public MyInterceptor myInterceptor() {
        return new MyInterceptor();
    }*/ 

    @Bean
    public RestTemplate restTemplate(AbcRequestInterceptor abcRequestInterceptor) {
        RestTemplate restTemplate = new RestTemplate();
        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
        interceptors.add(abcRequestInterceptor);
        //interceptors.add(myInterceptor); // I dont want to add this interceptor here
        restTemplate.setInterceptors(interceptors);
        return restTemplate;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-02-07 11:41:15

我认为在这种情况下您需要的是一个BeanPostProcessor,而不是一个Condition类。如果bean存在,它将允许您修改它。您可以创建如下所示的类,而不是RestTemplateConfiguration类和Condition

代码语言:javascript
复制
@Configuration
public class RestTemplatePostProcessor implements BeanPostProcessor {

    @Value("${com.pqr.you.rest.enabled:true}")
    private boolean transEnabled;

    @Bean
    MyInterceptor myInterceptor() {
        return new MyInterceptor();
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        if (bean instanceof RestTemplate) { // or you can check by beanName if you like
            final RestTemplate restTemplate = (RestTemplate) bean;

            if (transEnabled) {
                List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
                interceptors.add(myInterceptor());
                restTemplate.setInterceptors(interceptors);
            }
            return restTemplate;
        }
        return bean;
    }
}

注意,在您的代码中,您正在尝试创建另一个RestTemplate实例(名为"addInterceptor"),这似乎是不想要的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48625268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档