這篇文章給大家介紹怎么在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();
}
}關于怎么在SpringBoot中設置接口超時時間就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。