Java Lambda 的东西有点令人困惑。您需要编写一个 Lambda 库和一个可选库来处理基于 Amazon 的事件。
这些库在this page 上有所记录。
要在 maven 中使用 aws-lambda-java-core 库,您需要使用:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.0</version>
</dependency>
作为您的 Maven 依赖项。
要使用 aws-lambda-java-events(Lambda 事件库),请使用依赖项
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>2.2.5</version>
</dependency>
在你的pom.xml。
在您的示例中,com.amazonaws.services.lambda.runtime.Context 和 com.amazonaws.services.lambda.runtime.RequestHandler 来自 aws-lambda-java-core 库。 您的帖子看起来在第一次导入时遇到了复制/粘贴问题。
但是com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent 和com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent(同样,您的帖子中有错字)来自aws-lambda-java-events。这个库简化了对 AWS 事件的处理。您的代码看起来想要处理 API Gateway Proxy events。
但是请注意,通过引入 aws-lambda-java-events 库,您会引入大量 AWS 库。例如,即使您的 Lambda 只需要 API Gateway 事件,您仍然会引入对 S3 库的依赖,因为事件库也处理 S3 事件。因此,您的 Lambda 部署包将比您不引入它时大得多。
作为事件库的替代方案,您可以使用 JsonPath 之类的东西从事件中提取您想要的内容。您的 Lambda 将类似于:
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.jayway.jsonpath.JsonPath;
public class YourLambdaHandler implements RequestStreamHandler {
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) {
String accountId = JsonPath.read(inputStream, "$.requestContext.accountId");
它将从 API Gateway 事件中读取 accountId。
毫无疑问,这种方式需要更多的工作,但如果您只是从活动中取出几件物品,那么它可能是一种更轻的方式。
编辑
您似乎遇到了构建/IDE 问题。所以,我能展示的最少的代码首先是一个工作的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yourpackage.handler</groupId>
<artifactId>apigateway</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<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.2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
还有一个 Lambda 处理程序:
package com.yourpackage.handler;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
@SuppressWarnings("unused")
public class DemoHandler implements RequestHandler<APIGatewayProxyResponseEvent, String> {
public String handleRequest(APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent, Context context) {
return "hello";
}
}
你能从一个新环境开始尝试一下吗?