Overriding RestTemplateCustomizer applied to RestTemplate

Viewed 294

I am using RestTemplateCustomizer to configure common configuration for RestTemplate for one of my app. But now in one of my client class I need to override one of the custom Interceptor that I have with a different one.

I can get instance of RestTemplate without using RestTemplateBuilder and add all my configuration here and that should work, but i really don't want this approach.

The way I am thinking to solve this is get all interceptors attached to RestTemplate, find my custom Interceptor 1, remove it and add new custom Interceptor 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());
    }

It seems work fine but is there a better way to do this?

0 Answers
Related