我试图调试为什么我向AWS Lambda GateAPI的输入json无法解析JSON正文。不得不多次部署,在AWS上部署CloudFormation模板,然后进行调试,我在本地计算机中配置了AWS SAM以模拟无服务器环境。是代码
以下是代码和
的摘录public class FetchByChangeIdLambda implements RequestHandler<RequestBody, String> {
private static final String searchEndPoint=System.getenv("SEARCH_ENDPOINT");;
@Override
public String handleRequest(RequestBody changeId, Context context) {
System.out.println("Did it even entered"+" "+changeId.getChangeId());
if(context.getLogger()!=null) {
LambdaLogger lambdaLogger = context.getLogger();
lambdaLogger.log("Search End Point is" + " " + searchEndPoint);
lambdaLogger.log(String.format("Starting Search for Change Id,%s", changeId.getChangeId()));
return GetResponseByChangeId.getResponse(searchEndPoint, changeId.getChangeId(), lambdaLogger);
}
return GetResponseByChangeId.getResponse(searchEndPoint, changeId.getChangeId(), null);
}
POJO:
public class RequestBody {
private String changeId;
public RequestBody(String changeId) {
this.changeId=changeId;
}
public RequestBody() {
}
public String getChangeId() {
return changeId;
}
public void setChangeId(String changeId) {
this.changeId = changeId;
}
}
AWS本地SAM调试:
sam local -event event.json api -m GET -b“” | sam local invoke -d 5858 MicroserviceGetGateway
event.json的内容
{
"changeId":1212
}
错误消息:
java.lang.RuntimeException: An error occurred during JSON parsing
Caused by: java.io.UncheckedIOException: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'sam': was expecting ('true', 'false' or 'null')
at [Source: lambdainternal.util.NativeMemoryAsInputStream@7e07db1f; line: 1, column: 12]
Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'sam': was expecting ('true', 'false' or 'null')
at [Source: lambdainternal.util.NativeMemoryAsInputStream@7e07db1f; line: 1, column: 12]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1581)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:533)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:3451)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2610)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:841)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:737)
at com.fasterxml.jackson.databind.ObjectReader._initForReading(ObjectReader.java:378)
at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java
:1494) 在com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1102)
template.yaml
Transform: AWS::Serverless-2016-10-31
Description: Real Time Search in ES
Resources:
MicroserviceGetGateway:
Type: AWS::Serverless::Function
Properties:
Handler: com.here.realtime.fetch.requestbuilder.FetchByChangeIdLambda::handleRequest
Runtime: java8
CodeUri: build/distributions/SearchPayloadUsingId.zip
Policies: AWSLambdaBasicExecutionRole
Timeout: 30
MemorySize: 1024
Environment:
Variables:
SEARCH_ENDPOINT: xxxxxxxxxxxxxxxxx
Role: arn:aws:iam::240946934673:role/CMSBulkRole
Events:
GetResource:
Type: Api
Properties:
Path: /search
Method: get
Outputs:
RealTimeSearchAPI:
Description: URL for application
Value: !Sub 'https://${ServerlessRestApi}.execute-api.us-east-2.amazonaws.com/Stage/get/'
Export:
Name: RealTimeSearchAPI