1

When I use auto-configured bean of RestTemplateBuilder, the timeout field doesn't set as intended.

code:

    public WebHookService(RestTemplateBuilder restTemplateBuilder) {
        restTemplate = restTemplateBuilder
                .setConnectTimeout(Duration.ofSeconds(10))
                .setReadTimeout(Duration.ofSeconds(30))
                .build();

Result: Result


However When I set the custom bean of RestTemplateBuilder, timeout is set as Intended.

code:

    @Bean
    RestTemplateBuilder restTemplateBuilder() {
        return new RestTemplateBuilder();
    }
 
    // ...

    public WebHookService(RestTemplateBuilder restTemplateBuilder) {
        restTemplate = restTemplateBuilder
                .setConnectTimeout(Duration.ofSeconds(10))
                .setReadTimeout(Duration.ofSeconds(30))
                .build();

Result: enter image description here

As far as I know, In Spring Boot 1.4.0 application RestTemplateBuilder is available as a bean and you can simply use that.
So there should be no difference between using custom bean and auto-configured bean.
... But it didn't!

Anyone who knows why?

3
  • What is this WebHookService and how is it created/managed? Also, what version of Spring Boot are you using? Commented Sep 21, 2023 at 6:02
  • @AbhijitSarkar Thank you for pointing out the important aspect. WebHookService class is managed as @Service, and spring boot 3.1.0 is what I'm using. Commented Sep 21, 2023 at 6:17
  • Although I can think of at least 3 different ways to achieve the same thing, it should work as shown. The simplest debugging you can do it to put a breakpoint in the build method, and see what it does. You didn't show at which point you're checking the timeout values, and depending on the lifecycle, that might matter. Your question lacks debugging details. Commented Sep 21, 2023 at 6:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.