带有用于Java AWS Lambda的POJO输入处理程序的大写字段 [英] Capitalized fields with POJO Input Handlers for Java AWS Lambda

查看:183
本文介绍了带有用于Java AWS Lambda的POJO输入处理程序的大写字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此处描述的文档: http://docs.aws.amazon.com/lambda/latest/dg/java-programming-model-req-resp.html 可以创建自己的POJO来序列化Java AWS Lambda的输入和输出。

According to the documentation described here: http://docs.aws.amazon.com/lambda/latest/dg/java-programming-model-req-resp.html one can create its own POJO to serialize input and output for Java AWS Lambda.

但是对于字段大写的输入请求,它似乎不适用。例如,自定义资源lambda的输入格式如下所示:

However it appears doesn't properly work for input requests where fields are capitalized. For instance, the format of input for custom resource lambda looks like:

{"RequestType":"Create", 
"ServiceToken":"arn:aws:lambda:....", 
"ResponseURL":"https://cloudformation-custom-resource-response-e...",
...}

这可以通过这个简单的MCVE代码轻松测试:

This can be easily tested via this simple MCVE code:

package test;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class TestLambda implements RequestHandler<TestLambda.TestEvent, String> {

    private static final Logger logger = LogManager.getLogger(TestLambda.class);

    @Override
    public String handleRequest(TestEvent event, Context context) {
         logger.debug(event.toString());    
         return null;
    }

    public static final class TestEvent {
        private String key1;

        private String Key2;

        private String key3;

        public String getKey1() {
            return key1;
        }

        public void setKey1(String key1) {
            this.key1 = key1;
        }

        public String getKey2() {
            return Key2;
        }

        public void setKey2(String key2) {
            Key2 = key2;
        }

        public String getKey3() {
            return key3;
        }

        public void setKey3(String key3) {
            this.key3 = key3;
        }

        @Override
        public String toString() {
            return "TestEvent{" +
                    "key1='" + key1 + '\'' +
                    ", Key2='" + Key2 + '\'' +
                    ", key3='" + key3 + '\'' +
                    '}';
        }
    }
}

然后在AWS中创建测试这个lambda的控制台并在那里作为请求传递以下json:

Then create a test in AWS Console for this lambda and pass there as a request the following json:

{
  "key3": "value3",
  "Key2": "value2",
  "Key1": "value1"
}

日志中的结果将是:

2017-11-06 09:30:13 16849696-c2d5-11e7-80c3-150a37863c42 DEBUG TestLambda:15 - TestEvent{key1='null', Key2='null', key3='value3'}

有没有办法在不处理原始字节流的情况下对此输入进行反序列化,因为他们在该主题中建议?

Is any way to deserialize this input without dealing with raw byte stream as they suggest in that topic?


您不应该依赖序列化框架
的任何其他功能,例如注释。如果需要自定义序列化
行为,可以使用原始字节流来使用自己的
序列化。

You shouldn't rely on any other features of serialization frameworks such as annotations. If you need to customize the serialization behavior, you can use the raw byte stream to use your own serialization.

如果我们无法为任何类型的事件自由创建POJO,那么我认为这是Java AWS Lambdas的一个很大的限制。

This seems to me to be a great limitation of Java AWS Lambdas if we can not freely create a POJO for any type of event.

推荐答案

在pojo中,使字段公开,并且与json字段完全相同。这意味着你应该有上层驼峰案例字段,例如,

In the pojo, make the fields public and exactly the same case as the json fields. This means you should have upper camel case fields e.g.,

public class TestEvent {
    public String Key1;

    public String Key2;

    public String key3;
}

我无法解释为什么会这样,但我今天刚尝试了这个工作同事的建议,它的工作原理。我知道它看起来并不优雅。但至少它比反序列化流更少的代码行。

I cannot explain why this works, but I just tried this today based on a work colleague's suggestion and it works. I know it doesn't look elegant. But at least it's fewer lines of code than deserializing streams.

这篇关于带有用于Java AWS Lambda的POJO输入处理程序的大写字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆