Home > Mobile >  Spring Boot instinitate @Bean according to Condition
Spring Boot instinitate @Bean according to Condition

Time:05-18

im working in spring boot project where i want to instantiate a Restemplate Bean with Interceptors , my issue that i don't want to duplicate the code because there is just the header that changes for each conciguration. this is my code :

  @Bean
  @Qualifier("restTemplateOne")
  public RestTemplate restTemplateWithAccessToken() {
    return new RestTemplateBuilder()
      .interceptors((HttpRequest request, byte[] body, ClientHttpRequestExecution execution) -> {

        //this is the only header that i want to add for 
        request.getHeaders().set("MY_PARTICULAR_HEADER", "my value");

       request.getHeaders().set(HttpHeaders.AUTHORIZATION,"my auth value"); 

        return execution.execute(request, body);

      }).build();
  }

  @Bean
  @Qualifier("restTemplateTwo")
  public RestTemplate restTemplateWithIdToken() {
    return new RestTemplateBuilder()
      .interceptors((HttpRequest request, byte[] body, ClientHttpRequestExecution execution) -> {

        request.getHeaders().set(HttpHeaders.AUTHORIZATION,"my auth value");

        return execution.execute(request, body);

      }).build();
  }
  
  
  
  @Autowired
  @Qualifier("restTemplateOne")
  private RestTemplate restTemplateOne;
  
  @Autowired
  @Qualifier("restTemplateTwo")
  private RestTemplate restTemplateTwo;

do you have any idea how i can optimize code to avoid duplication . something like adding a parameter to the method and adding the header or not according to the condition.

Thanks in advance.

CodePudding user response:

Just extract and parameterize your interceptor:

    @Bean
    @Qualifier("restTemplateOne")
    public RestTemplate restTemplateWithAccessToken() {
        return new RestTemplateBuilder()
                .interceptors(new CustomClientHttpRequestInterceptor(true))
                .build();
    }

    @Bean
    @Qualifier("restTemplateTwo")
    public RestTemplate restTemplateWithIdToken() {
        return new RestTemplateBuilder()
                .interceptors(new CustomClientHttpRequestInterceptor(false))
                .build();
    }

    private static class CustomClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
        private boolean needParticularHeader;

        public CustomClientHttpRequestInterceptor(boolean needParticularHeader) {
            this.needParticularHeader = needParticularHeader;
        }

        @Override
        public ClientHttpResponse intercept(HttpRequest request,
                                            byte[] body,
                                            ClientHttpRequestExecution execution) throws IOException {
            if (needParticularHeader) {
                //this is the only header that i want to add for
                request.getHeaders().set("MY_PARTICULAR_HEADER", "my value");
            }

            request.getHeaders().set(HttpHeaders.AUTHORIZATION, "my auth value");

            return execution.execute(request, body);

        }
    }
  • Related