AWSMarketplaceMeteringClientBuilder.withRegion() 不可见

AWSMarketplaceMeteringClientBuilder.withRegion() is not visible

我正在使用 Java 的 AWS SDK 在 AWS Metering 服务中使用。当我尝试使用 AWSMarketplaceMeteringClientBuilder 创建一个 AWSMarketplaceMeteringClient 时,我发现如果我使用 withRegion(Region region) 方法,我会得到以下编译时错误:

The method withRegion(Region) from the type AwsClientBuilder<AWSMarketplaceMeteringClientBuilder,AWSMarketplaceMetering> is not visible

客户端代码如下:

AWSMarketplaceMeteringClient metClient = (AWSMarketplaceMeteringClient) AWSMarketplaceMeteringClientBuilder
        .standard()
        .withRegion(Regions.getCurrentRegion())
        .withCredentials(InstanceProfileCredentialsProvider.getInstance())
        .build();

当我尝试直接使用 AWSMarketplaceMeteringClientsetRegion(Region region) 方法时,出现以下运行时错误:

Exception in thread "main" java.lang.UnsupportedOperationException: Client is immutable when created with the builder.
    at com.amazonaws.AmazonWebServiceClient.checkMutability(AmazonWebServiceClient.java:854)
    at com.amazonaws.AmazonWebServiceClient.setRegion(AmazonWebServiceClient.java:349)

那么withRegion(Region region)方法应该怎么用呢?

如果我们仔细观察,AwsClientBuilder class 有以下方法:

public final Subclass withRegion(Regions region) { }
public final Subclass withRegion(String region) { }
private Subclass withRegion(Region region) { }

我试图使用方法 withRegion(Region region),它在这个基础 class 中是私有的。所以我们应该使用方法 withRegion(Regions region) [注意:参数是 Regions 而不是 Region]。使用此方法解决了我的问题。