欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

SpringBoot设置接口超时时间的方法

时间:2021-10-21 09:11:04|栏目:JAVA代码|点击:

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();
 }
}

上一篇:详解Spring ApplicationContext加载过程

栏    目:JAVA代码

下一篇:Java 遍历list和map的方法

本文标题:SpringBoot设置接口超时时间的方法

本文地址:http://www.codeinn.net/misctech/180743.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有