noclassdeffounderror:JavaSDK中的AmazonSClient

jqjz2hbq  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(256)

当我尝试示例化awssnsclient时,我得到了一个错误类定义not found。此代码没有在aws lambda及其本地系统中运行。这是我的java代码

18->   AmazonSNSClient snsClient = new AmazonSNSClient(credentials);
        snsClient.setRegion(Region.getRegion(Regions.));
        ObjectMapper Obj = new ObjectMapper();
        PublishRequest publishRequest = new PublishRequest();
        publishRequest.setTargetArn("");
        publishRequest.setMessage("");
        PublishResult publishResponse = snsClient.publish(publishRequest);

我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cmds</groupId>
    <artifactId></artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name></name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk-bom</artifactId>
                <version>1.11.379</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-kubernetes-config</artifactId>
            <version>1.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-flow</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>amazon-sqs-java-messaging-lib</artifactId>
            <version>1.0.8</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-sns</artifactId>
            <version>1.11.721</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>

    </build>

</project>

当java代码执行时,我收到了以下错误
java.lang.noclassdeffounderror:com/amazonaws/services/sns/amazonsClient位于com.cmds.booking.infrastructure.snsservice.publishmessage(snsservice)。java:18)
有依赖性问题吗?我哪里出错了?

eyh26e7m

eyh26e7m1#

我已经尽我所能检查了您的java代码片段,我注意到了两个问题:
(i)以下行不完整:

snsClient.setRegion(Region.getRegion(Regions.));

应改为:

snsClient.setRegion(Region.getRegion(Regions.US_EAST_1)); //US_EAST_1 can be replaced with your desired region

(ii)以下声明被否决:

new AmazonSNSClient(credentials)

以及

snsClient.setRegion(Region.getRegion(Regions.fromName(region)));

相反,应按以下方式创建aws服务客户端:
如果您使用的是凭据文件,则可以使用以下选项:

BasicAWSCredentials basicAwsCredentials = new BasicAWSCredentials(AccessKey,SecretAccessKey);
AmazonSNS snsClient = AmazonSNSClient
                    .builder()
                    .withRegion(your_region)
                    .withCredentials(new AWSStaticCredentialsProvider(basicAwsCredentials))
                    .build();

如果要使用instanceprofilecredentialprovider,则可以使用以下选项:

AmazonSNS sns = AmazonSNSClientBuilder
                .standard()
                .withCredentials(new InstanceProfileCredentialsProvider(true))
                .build();

更多信息可以在aws java开发者指南中找到
希望这有帮助!

相关问题