SoFunction
Updated on 2025-03-03

How to set the interface timeout time in SpringBoot

There are two ways to set the interface access timeout time in SpringBoot

1. Add -timeout=20000 to the configuration file, which means setting the timeout time to 20000ms, that is, 20s.

2. Another type is to add it to the config configuration class:

public class WebMvcConfig extends WebMvcConfigurerAdapter {
 @Override
  public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
    (20000);
    (timeoutInterceptor());
  }
 @Bean
 public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
   return new TimeoutCallableProcessingInterceptor();
 }
}

PS: SpringBoot Rest API Set timeout

The project has an open API, and external network access often timeouts. Shortly after I first came into contact with spring boot, the built-in tomcat does not set the request timeout timeout in the original.

Later, I checked some information and added -timeout=20000 to the configuration file, which means setting the timeout time to 20000ms, that is, 20s. The timeout problem does not happen much.

There is another way to set it up, as follows:

public class WebMvcConfig extends WebMvcConfigurerAdapter {
 @Override
  public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
    (20000);
    (timeoutInterceptor());
  }
 @Bean
 public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
   return new TimeoutCallableProcessingInterceptor();
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.