gpt4 book ai didi

java - 大文件的 Wiremock withBodyFile OutOfMemoryError

转载 作者:行者123 更新时间:2023-12-05 05:39:40 26 4
gpt4 key购买 nike

在以下 stub 请求中,我使用 withBodyFile 使用流从本地磁盘上的特定位置加载大文件(>300MB):

public void mockGetUrlContent(String url) {
stubFor(get(urlEqualTo(url))
.willReturn(ok()
.withBodyFile(FilenameUtils.getName(url))));
}

调用 stub 后,wiremock 会尝试完成服务事件,并且为了记录响应,它使用 from method下面:

  public static LoggedResponse from(Response response) {
return new LoggedResponse(
response.getStatus(),
response.getHeaders() == null || response.getHeaders().all().isEmpty()
? null
: response.getHeaders(),
response.getBody(),
response.getFault());
}

当到达 wiremock 的 Response getBody method它将流转换为 byte[] 并在那个点上发生 OutOfMemoryError(Java 堆空间)。:

  public byte[] getBody() {
try (InputStream stream = bodyStreamSource == null ? null : getBodyStream()) {
return stream == null ? null : ByteStreams.toByteArray(stream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

关于我做错了什么有什么想法吗?

非常感谢您的帮助!

最佳答案

WireMock 2.34.0 对此有解决方案。

为确保将记录的响应主体限制为特定大小,您需要使用新配置参数启动 WireMock,以字节为单位指定最大大小

独立运行时添加 CLI 参数:

--logged-response-body-size-limit=1000000

在 Java 中运行时,将以下内容添加到配置构建器中:

wireMockConfig().maxLoggedResponseSize(1000000)

关于java - 大文件的 Wiremock withBodyFile OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72628047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com