SpringBoot設置接口超時時間的方法

這篇文章主要介紹了SpringBoot設置接口超時時間的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨着小編來一起學習學習吧

SpringBoot設置接口訪問超時時間有兩種方式

一、在配置文件application.properties中加了spring.mvc.async.request-timeout=20000,意思是設置超時時間爲20000ms即20s,

二、還有一種就是在config配置類中加入:

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

PS:SpringBoot Rest Api 設置超時時間

項目有一對外開放api,外網訪問經常出現超時,剛接觸spring boot不久,內置的tomcat不像原先那樣在server.xml中設置request超時時間。

後來查了些資料,在配置文件application.properties中加了spring.mvc.async.request-timeout=20000,意思是設置超時時間爲20000ms即20s,超時問題的確不怎麼發生了。

還有另外一種設置方式,如下:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章