我目前正在尝试编写针对AWS的编码,并且在尝试订阅用户到SNS主题时遇到了一个问题。
所以我所拥有的是一个lambda表达式,它应该接收一封电子邮件并将其发送给SNS。我可以调用表达式并且工作正常,但是当它尝试创建AmazonSNSClient
时,我得到java.lang.NoClassDefFoundError: com/amazonaws/services/sns/AmazonSNSClient
代码非常简单:
public String handleRequest(SubscriptionRequest request, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("Subscribing to list with email:" + request.email);
logger.log("Creating SNS client");
AmazonSNSClient snsClient = new AmazonSNSClient(new AnonymousAWSCredentials());
snsClient.setRegion(Region.getRegion(Regions.EU_WEST_1));
//subscribe to an SNS topic
SubscribeRequest subRequest = new SubscribeRequest(TOPIC_ARN, "email", request.email);
logger.log("Sending request");
snsClient.subscribe(subRequest);
return "OK";
}
其中也可能存在大量其他错误,但这是我到目前为止所获得的,具有以下依赖性
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sns</artifactId>
<version>1.11.328</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.11.328</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.328</version>
</dependency>
</dependencies>
我认为我添加了比实际需要更多的依赖项,但由于我不知道问题出在哪里,我只是尝试添加任何有意义的内容。
所以问题是:
我错过了什么,导致NoClassDefFoundError
的原因是什么?
修改
由于不推荐AmazonSNSClient
的构造函数,我将行更改为
logger.log("Creating SNS client");
AmazonSNS snsClient = AmazonSNSClientBuilder.defaultClient();
但是我仍然遇到错误,因为它找不到AmazonSNSClientBuilder
。
这几乎就好像它没有将sns依赖项添加到jar文件中,这很奇怪,因为lambda依赖项似乎工作正常
答案 0 :(得分:0)
在这里找到解决方案: AWS Lambda NoClassDefFoundError
必须使用这段pom配置进入uberjar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>