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,超時問題的確不怎么發(fā)生了。
還有另外一種設置方式,如下:
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();
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
RestTemplate對HttpClient的適配源碼解讀
這篇文章主要為大家介紹了RestTemplate對HttpClient的適配源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10

