带有杰克逊注释的AWS Lambda json反序列化 [英] AWS Lambda json deserialization with jackson annotations

查看:121
本文介绍了带有杰克逊注释的AWS Lambda json反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用json主体调用aws lambda.因此json的字段与POJO中的字段名称不同.所以我要做的是在字段上添加@JsonProperty来告诉杰克逊json中的名字是什么.但是由于某种原因,它似乎无法识别它们,并且所有字段均为空.如果我传递一个具有与POJO相同的字段名称的json,那么它将起作用.这是我的课:

I'm calling an aws lambda with a json body. So the fields of the json are with different name from the ones in the POJO. So what I did is to add @JsonProperty on the fields to tell jackson what are the names in json. But for some reason it seems that it doesn't recognize them and all the fields are null. If I pass a json with the same field names as the POJO it's working. Here's my class:

public class Event implements Identifiable {

    @JsonProperty("distinct_id")
    private String distinctId;

    @JsonProperty("user_id")
    private Integer userId;

    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime eventDateTime;

    //Here are the getters and setters
}

如果我通过

{"distinct_id":"123", "user_id":123, "dt":"2017-01-04T08:45:04+00:00"} 

所有字段均为空,并且具有distinctId,userId,eventDateTime,它可以进行序列化,除了它也不能识别我的自定义序列化器/反序列化器,但这实际上是相同的问题.

all the fields are null and with distinctId, userId, eventDateTime it's serializing ok with the exception that it also doesn't recognize my custom serializers/deserializers but this actually is the same problem.

我的结论是,由于某些原因,aws jackson无法使用注释,但没有任何意义.

My conclusion is that for some reason the aws jackson is not working with the annotations but it doesn't make sense.

推荐答案

所以我找到了一种方法.您需要实现RequestStreamHandler,它可以为您提供可以使用的输入和输出流:

So I found a way to do this. You need to implement RequestStreamHandler which gives you input and output streams which you can work with:

import com.amazonaws.services.lambda.runtime.RequestStreamHandler

public class ChartHandler implements RequestStreamHandler {
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        DeserializationClass deserializedInput = objectMapper.readValue(inputStream, DeserializationClass.class)
        objectMapper.writeValue(outputStream, deserializedInput); //write to the outputStream what you want to return
    }

}

具有输入和输出流使您独立于用于解析它的格式和框架.

Having the input and output streams makes you independent of the format and frameworks you use to parse it.

这篇关于带有杰克逊注释的AWS Lambda json反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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