有一个 API 网关使用 lambda 代理调用 lambda 函数。
以下工作正常,正文被记录并发回:
package com.dapper.cloud.function;
import java.util.Map;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
public class GrantJwt implements RequestHandler<Map<String, Object>, APIGatewayProxyResponseEvent>{
@Override
public APIGatewayProxyResponseEvent handleRequest(Map<String, Object> input, Context context){
System.out.println(input.get("body").toString());
return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody(input.get("body").toString());
}
}
当我更新它以使用 ObjectMapper
时,如下所示:
package com.dapper.cloud.function;
import java.util.Map;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.fasterxml.jackson.databind.ObjectMapper;
public class GrantJwt implements RequestHandler<Map<String, Object>, APIGatewayProxyResponseEvent>{
@Override
public APIGatewayProxyResponseEvent handleRequest(Map<String, Object> input, Context context){
ObjectMapper m = new ObjectMapper();
System.out.println(input.get("body").toString());
return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody(input.get("body").toString());
}
}
日志不显示正文,响应为:
{
"message": "Internal server error"
}
我可以在 AWS Lambda 中使用 Jackson 吗?
父 POM
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<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.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
</dependencies>
子构建
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
请您参考如下方法:
虽然很愚蠢,但如果这对任何人有帮助,我会非常高兴。
ObjectMapper 不是问题。我使用 travis-ci 进行了几次部署,显然,如果未设置内存大小和超时,则使用默认的 128 mb 和 3 秒。这与新 lambda 的默认值(512 mb 和 15 秒)不同。
我在 travis 论坛上提出了更改此设置的请求。