调用DynamoDB遇SdkClientException:类路径多HTTP实现问题求助
错误信息
Multiple HTTP implementations were found on the classpath. To avoid non-deterministic loading implementations, please explicitly provide an HTTP client via the client builders, set the software.amazon.awssdk.http.service.impl system property with the FQCN of the HTTP service to use as the default, or remove all but one HTTP implementation from the classpath
问题原因
在Quarkus环境中,dynamodb依赖会自动引入Quarkus适配的AWS HTTP客户端实现,而你额外添加了url-connection-client依赖,导致classpath中存在多个HTTP客户端实现,AWS SDK无法确定默认使用哪一个,因此抛出该异常。
解决方法
方法一:移除多余的HTTP客户端依赖
直接删除pom.xml中的url-connection-client依赖,Quarkus已为AWS SDK提供了适配的HTTP客户端,无需额外引入:
<!-- 删除以下依赖 --> <!-- <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>url-connection-client</artifactId> </dependency> -->
方法二:显式指定HTTP客户端
若需保留url-connection-client,在构建DynamoDbClient时显式指定该客户端:
@Singleton public class DynamoClientConfig { @Produces @ApplicationScoped public DynamoDbClient getClientDb() { return DynamoDbClient.builder() .region(Region.SA_EAST_1) .httpClient(UrlConnectionHttpClient.create()) // 显式指定使用UrlConnection客户端 .build(); } }
方法三:通过系统属性指定默认客户端
在Quarkus的application.properties中添加系统属性,指定默认HTTP客户端的全类名:
quarkus.system-properties."software.amazon.awssdk.http.service.impl"=software.amazon.awssdk.http.urlconnection.UrlConnectionHttpService
内容的提问来源于stack exchange,提问作者user19573093

