小编afs*_*s35的帖子

使用带有阻塞/同步请求的 Spring 的 WebClient 通过 try/catch 捕获异常

我需要发出同步的阻塞请求,并且我正在使用 SpringWebClient而不是 Spring RestTemplate,因为后者已被弃用。在这种情况下,我不需要反应式功能,我只想以简单的方式使用 REST API,而不包含额外的依赖项。

\n

我有以下代码,其按预期工作:

\n
MyObject object = webClient.get()\n    .uri( myUri )\n    .retrieve()\n    .bodyToMono( MyObject.class )\n    .block()\n
Run Code Online (Sandbox Code Playgroud)\n

但是,我需要处理无法连接到 API 或连接但收到 4xx/5xx 代码时的情况。

\n

因此,最简单的方法是将调用放入 try/catch 中,并捕获 Spring 的,如果它获得 4xx/5xx 代码,WebClientResponseException则会抛出该错误:.bodyToMono

\n
import org.springframework.web.reactive.function.client.WebClientResponseException;\n\ntry {\n\n    MyObject object = webClient.get()\n        .uri( myUri )\n        .retrieve()\n        .bodyToMono( MyObject.class )\n        .block()\n\n}\n\ncatch ( WebClientResponseException e ) {\n\n    // Logic to handle the exception.\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这工作正常,但如果连接被拒绝(例如,如果 URL 错误或服务关闭),则不起作用。在这种情况下,我在控制台中看到以下内容:

\n
\n

reactor.core.Exceptions$ErrorCallbackNotImplemented:\nio.netty.channel.AbstractChannel$AnnotatedConnectException:\nfinishConnect(..) 失败: 连接被拒绝: /127.0.0.1:8090 …

java rest spring spring-boot spring-webclient

10
推荐指数
1
解决办法
1万
查看次数

GCP - Cloud Function 在同一项目中无法从 Artifact Registry 中找到 Python 包

我一直在尝试 GCP 的 ArtifactRegistry,它目前处于 Python 包的 alpha 版本。

我通过密钥环和我的服务帐户进行身份验证,如文档中所述。

我可以使用 Twine 成功上传包,并且可以通过安装以下内容成功将其下载到本地 Python 项目requirements.txt

--extra-index-url https://my-region-pypi.pkg.dev/my-project/my-python-repo/simple/
my-package
Run Code Online (Sandbox Code Playgroud)

但是,当我将最小的云功能部署到与 Artifact Registry 相同的项目时(如上requirements.txt所示),部署失败并显示以下输出:

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: `pip_download_wheels` had stderr output:
ERROR: Could not find a version that satisfies the requirement my-package (from -r requirements.txt (line 2)) (from versions: none)
ERROR: No matching distribution found for my-package (from -r requirements.txt (line 2))
Run Code Online (Sandbox Code Playgroud)

我尝试了两者--extra-index-url,只是简单--index-url,没有区别。我还尝试使用以下命令安装密钥环依赖项requirements.txt

--extra-index-url https://my-region-pypi.pkg.dev/my-project/my-python-repo/simple/
keyring
keyrings.google-artifactregistry-auth
my-module …
Run Code Online (Sandbox Code Playgroud)

python google-cloud-platform google-cloud-functions google-artifact-registry

6
推荐指数
2
解决办法
2603
查看次数