Spring Boot Application - what is default timeout for any rest API endpoint or a easy config to control all endpoint timeout

Viewed 88648

I am using current Spring boot version (1.4.x) and wondering if it has any default timeout for api calls. I have tested it by putting breakpoints but it was keep waiting and didn't time-out. I was also trying to configure default timeout for all my spring-boot apps by using some annotation or yml settings.

I found couple of alternatives (one of them here) but using callable actually adding extra non-business logic code where setting something in xml bean is out of fashion in latest spring boot applications.

5 Answers

You can create a configuration file using annotation @Configuration

For using RestTemplate:

` @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {

        return new RestTemplateBuilder()
                .setConnectTimeout(Duration.ofMillis(30000))
                .setReadTimeout(Duration.ofMillis(30000))
      

      .build();
}`

For WebClient Timeouts:

 @Bean
    public WebClient webClient() {
        return WebClient.builder()
                .clientConnector(new ReactorClientHttpConnector(
                        HttpClient.create().responseTimeout(Duration.ofMillis(30000))
                ))
                .build();
    }

After the configuration is done, in a controller or any other file where you want to use this RestTemplate or WebClient just autowire it like:

@Autowired
private RestTemplate restTemplate;

or

@Autowired
private WebClient webClient;
Related