0

I'm learning about RestTemplate in Spring Boot project and I want to log the value of connectTimeout property.

To set this value I can do this implementation:

HttpComponentsClientHttpRequestFactory rf =
        (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
rf.setConnectTimeout(3000);

But I want to set it in application.yml like this:

  communication:
    http:
      client:
        connectTimeout: '3000'

And then, in the configuration class I want to log this value. How can I do this to obtain the value of the connectTimeout in the configuration class? I cannot do rf.getConnectTimeout(); because this method doesn't exist. And another question, how Spring set up this connectTimeout value that I added it in the application.yml? Any feedback will be apreciated!

elvis
  • 956
  • 9
  • 33
  • 56

1 Answers1

0

AFAIK, there isn't a simple 'set property X' answer.

What I do is create my own @ConfigurationProperties class e.g. RestTemplateProperties , register it etc. RestTemplate bean and customise my RestTemplate there.

@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder, RestTemplateProperties restTemplateProperties) {
  return restTemplateBuilder
    .setConnectTimeout(restTemplateProperties.getConnectionTimeout())
    .setReadTimeout(restTemplateProperties.getReadTimeout())
    .build();
}

Then you can set the values in your properties file.

NB: this is isn't exactly how I do it, but hopefully you get the idea.

jeremyt
  • 510
  • 4
  • 6