Google Cloud KMS java.lang.NoSuchMethodError:com.google.common.base.Preconditions.checkArgument(ZLjava / lang / String; CLjava / lang / Object;)V

时间:2019-09-26 01:15:24

标签: java gradle google-cloud-kms

我正在将Java客户端库用于KMS。 KeyManagementServiceClient.create()方法shown here抛出上述异常。

番石榴依赖项看起来还不错-27.1-jre

public String encrypt(String plaintext) throws IOException {
        byte[] ciphertext = encrypt(PROJECT_ID, LOCATION_ID, KEYRING_ID, KEY_ID, plaintext.getBytes());
        return ciphertext.toString();
    }


    /**
     * Encrypts the given plaintext using the specified crypto key.
     */
    private static byte[] encrypt(
            String projectId, String locationId, String keyRingId, String cryptoKeyId, byte[] plaintext)
            throws IOException {

        // Create the KeyManagementServiceClient using try-with-resources to manage client cleanup.
        try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {

            // The resource name of the cryptoKey
            String resourceName = CryptoKeyName.format(projectId, locationId, keyRingId, cryptoKeyId);

            // Encrypt the plaintext with Cloud KMS.
            EncryptResponse response = client.encrypt(resourceName, ByteString.copyFrom(plaintext));

            // Extract the ciphertext from the response.
            return response.getCiphertext().toByteArray();
        }
    }
java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V

    at io.grpc.Metadata$Key.validateName(Metadata.java:629)
    at io.grpc.Metadata$Key.<init>(Metadata.java:637)
    at io.grpc.Metadata$Key.<init>(Metadata.java:567)
    at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:742)
    at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:737)
    at io.grpc.Metadata$Key.of(Metadata.java:593)
    at io.grpc.Metadata$Key.of(Metadata.java:589)
    at com.google.api.gax.grpc.GrpcHeaderInterceptor.<init>(GrpcHeaderInterceptor.java:60)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:220)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:193)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:185)
    at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:160)
    at com.google.cloud.kms.v1.stub.GrpcKeyManagementServiceStub.create(GrpcKeyManagementServiceStub.java:370)
    at com.google.cloud.kms.v1.stub.KeyManagementServiceStubSettings.createStub(KeyManagementServiceStubSettings.java:333)
    at com.google.cloud.kms.v1.KeyManagementServiceClient.<init>(KeyManagementServiceClient.java:155)
    at com.google.cloud.kms.v1.KeyManagementServiceClient.create(KeyManagementServiceClient.java:136)
    at com.google.cloud.kms.v1.KeyManagementServiceClient.create(KeyManagementServiceClient.java:127)
    at com.project.encryption.KmsEncryption.encrypt(KmsEncryption.java:38)
    at com.project.KmsEncryption.encrypt(KmsEncryption.java:21)

我希望它能返回编码后的字符串,因为在相同的项目设置和配置下,命令行指令可以正常工作。

1 个答案:

答案 0 :(得分:0)

第3方库的问题:

  java.lang.NoSuchMethodError:   com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V

让我们尝试看看番石榴的依赖性。对我来说,已通过更新番石榴解决了该问题:

 <dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>21.0</version>
</dependency>