java - aws lambda没有将json请求转换为POJO

标签 java amazon-web-services aws-lambda aws-sdk aws-sam-cli

我有一个 lambda 函数:

package org.smarter.note;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class AddNoteRequestHandler implements RequestHandler<NewNoteRequest, Note> {

    private NoteRepository notes;
    private LambdaLogger logger;

    @Override
    public Note handleRequest(NewNoteRequest newNote, Context context) {
        init(context);

        log("creating note: " + newNote.toJson());

        notes.create(newNote);

        return new Note();
    }

    private void log(String message) {
        logger.log(String.format("%s\n", message));
    }

    private void init(Context context) {
        this.logger = context.getLogger();
        this.notes = new NoteRepository();
    }
}

然后当我使用 sam local 测试 lambda 并发送请求时:

{
    "title": "hello",
    "content": "body"
}

我期望 json 自动转换为 NewNoteRequest,但事实并非如此。这是日志:

You can now browse to the above endpoints to invoke your functions.
You do not need to restart/reload SAM CLI while working on your functions,
changes will be reflected instantly/automatically. You only need to restart
SAM CLI if you update your AWS SAM template.

2017/12/28 11:06:57 Invoking org.smarter.note.AddNoteRequestHandler (java8)
2017/12/28 11:06:57 Decompressing /Work/smarter-serverless/build/distributions/smarter-serverless.zip
2017/12/28 11:06:57 Mounting /private/var/folders/gv/m43y5g9x1xdc9f2kc2p0kpz00000gp/T/aws-sam-local-1514430417742696978 as /var/task:ro inside runtime container
START RequestId: bf0f77d3-198d-4211-ab9e-4c85a7c5de22 Version: $LATEST
creating note: {title=null, content=null}

我的 sam 本地模板:

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  notes:
    Type: AWS::Serverless::Function
    Properties:
      Handler: org.smarter.note.AddNoteRequestHandler
      CodeUri: ./build/distributions/smarter-serverless.zip
      Runtime: java8
      Events:
        PostEvent:
          Type: Api
          Properties:
            Path: /notes
            Method: post

POJO:

package org.smarter.note;

import com.amazonaws.services.dynamodbv2.document.Item;

import java.util.HashMap;
import java.util.Map;

class NewNoteRequest implements DynamoDBItem, Json {
    private String title;
    private String content;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public Item item() {
        return new Item()
                .withString("title", this.title)
                .withString("content", this.content);
    }

    @Override
    public Map<String, Object> toJson() {
        HashMap<String, Object> json = new HashMap<String, Object>();
        json.put("title", this.title);
        json.put("content", this.content);
        return json;
    }
}

依赖项还有:

dependencies {
    compile(
            "com.amazonaws:aws-lambda-java-core:1.1.0",
            "com.amazonaws:aws-lambda-java-events:1.1.0"
    )
}

我不知道哪部分是错误的,AWS文档说它可以直接进行映射。请帮忙。

最佳答案

我确实解决了这个问题的我的版本。在“集成请求”下,我检查了“访问控制允许凭据”。取消选中它会导致 POJO 被填充。

关于java - aws lambda没有将json请求转换为POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48001248/

相关文章:

java - 如何流畅比较ArrayList元素?

amazon-web-services - Elastic Load Balancing 向另一个服务发出请求时出错

amazon-web-services - 将文件通过 FTP 传输到 AWS S3

aws-lambda - 如何在 AWS 中设置 lambda 函数以安装某些包和依赖项

python - 为什么这个 AWS Lambda 函数返回 JSON 而不是 HTML?

java - 在 Portlet For IBM 中上传带有响应的文件

java - Portlet URL 附加 session ID?

java - JTable 中的选定行以及排序

amazon-web-services - 带有 Lambda 代理的 AWS API Gateway 总是产生 base64 字符串响应

javascript - 从api获取json时变量未定义