【问题标题】:SpringBoot: Timeout when the endpoint doesn't return a response [duplicate]SpringBoot:端点不返回响应时超时[重复]
【发布时间】:2022-01-11 20:54:51
【问题描述】:

我是 SpringBoot 的新手,正在寻找一种在端点需要超过 3 秒才能返回响应时超时的方法。我尝试通过添加属性“server.servlet.session.timeout”,但仍然没有运气。如何做到这一点?谢谢。

@GetMapping("/api")
public Data getData(){

    Thread.sleep(10000);

    return ....;
}

Application.properties

server.servlet.session.timeout=3s

【问题讨论】:

  • 不,“servlet.session”不适合“请求”..

标签: java spring spring-boot


【解决方案1】:

不确定您的 http 客户端是什么样的。 (RestTemplate/WebClient)。但是使用 WebClient,您可以根据需要创建 TimeOut。

HttpClient client = HttpClient.create()
  .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000);

详细说明请参考:

https://www.baeldung.com/spring-webflux-timeout

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 2019-09-10
    • 1970-01-01
    • 2017-04-28
    相关资源
    最近更新 更多