java.lang.ClassNotFoundException: com.amazonaws.transform.EnhancedJsonErrorUnmarshaller

java.lang.ClassNotFoundException: com.amazonaws.transform.EnhancedJsonErrorUnmarshaller

尝试连接到机密管理器时,我的代码抛出此异常。我正在尝试创建 Secrets Manager 客户端。

AWSSecretsManager client = 
    AWSSecretsManagerClientBuilder.standard()
        .withRegion(region)
        .build();

Pom.xml中添加了以下依赖项。

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-secretsmanager</artifactId>
    <version>1.11.965</version>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.11.965</version>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-core</artifactId>
    <version>1.11.965</version>
</dependency>

Amazon 建议转向 Java V2 的 AWS SDK。您可以在 Github here.

中找到 Secret Manager V2 代码

包含依赖项的 POM 文件位于 SecretManager 文件夹的 Github 中。

V2 代码已经过多次测试,此代码有效:

package com.example.secrets;

//snippet-start:[secretsmanager.java2.create_secret.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
import software.amazon.awssdk.services.secretsmanager.model.CreateSecretRequest;
import software.amazon.awssdk.services.secretsmanager.model.CreateSecretResponse;
import software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException;
//snippet-end:[secretsmanager.java2.create_secret.import]

/**
 * To run this AWS code example, ensure that you have setup your development environment, including your AWS credentials.
 *
 * For information, see this documentation topic:
 *
 *https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */


public class CreateSecret {

    public static void main(String[] args) {

       final String USAGE = "\n" +
                "Usage:\n" +
                "    CreateSecret  <secretName> <secretValue> \n\n" +
                "Where:\n" +
                "    secretName - the name of the secret (for example, tutorials/MyFirstSecret). \n"+
                "    secretValue - the secret value. \n";

        if (args.length != 2) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String secretName = args[0];
        String secretValue= args[1];

        Region region = Region.US_EAST_1;
        SecretsManagerClient secretsClient = SecretsManagerClient.builder()
                .region(region)
                .build();

       String secretARN = createNewSecret(secretsClient, secretName, secretValue);
       System.out.println("The secret ARN is "+ secretARN);
       secretsClient.close();
    }

    //snippet-start:[secretsmanager.java2.create_secret.main]
    public static String createNewSecret( SecretsManagerClient secretsClient, String secretName, String secretValue) {

        try {
            CreateSecretRequest secretRequest = CreateSecretRequest.builder()
                .name(secretName)
                .description("This secret was created by the AWS Secret Manager Java API")
                .secretString(secretValue)
                .build();

            CreateSecretResponse secretResponse = secretsClient.createSecret(secretRequest);
            return secretResponse.arn();

        } catch (SecretsManagerException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        return "";
    }
    //snippet-end:[secretsmanager.java2.create_secret.main]
} 

@smac2020 提供的解决方案对我没有帮助,因为我处理的不是机密,而是 AWS Cognito。 这就是我发现的 - 将来可能对某人有帮助

我在更新到最新版本的 cognitoip 时遇到此错误 1.12.167

   implementation group:  'io.awspring.cloud', name: 'spring-cloud-starter-aws-parameter-store-config', version: 1.12.167

这会自动 downloads/is 与 aws-java-sdk-core[的 旧版本 捆绑在一起=49=]依赖项。

EnhancedJsonErrorUnmarshaller是一个新的class,最新版本是aws-java-sdk-core,默认版本中没有cognitoip 与.

捆绑在一起

解决方案:手动更新aws-java-sdk-core以匹配cognitoip[=38=的版本].

 implementation group: 'com.amazonaws', name: 'aws-java-sdk-core', version: '1.12.167'

里面有 EnhancedJsonErrorUnmarshaller