好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

SpringBoot设置接口访问超时时间有两种方式

一、在配置文件application.properties中加了spring.mvc.async.request-timeout=20000,意思是设置超时时间为20000ms即20s,

二、还有一种就是在config配置类中加入:

?

1

2

3

4

5

6

7

8

9

10

11

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,超时问题的确不怎么发生了。

还有另外一种设置方式,如下:

?

1

2

3

4

5

6

7

8

9

10

11

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

  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/qq_35860138/article/details/88941558

查看更多关于SpringBoot设置接口超时时间的方法的详细内容...

  阅读:51次