我正在调用一个带有 json 正文的 aws lambda。所以 json 的字段与 POJO 中的字段名称不同。所以我所做的是在字段上添加@JsonProperty 来告诉 jackson json 中的名称是什么。但出于某种原因,它似乎无法识别它们并且所有字段均为空。如果我传递一个与它工作的 POJO 具有相同字段名称的 json。这是我的类(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"}
所有字段均为 null,并且具有 distinctId、userId、eventDateTime,它的序列化正常,但它也无法识别我的自定义序列化器/反序列化器,但这实际上是同一个问题。
我的结论是,出于某种原因,aws jackson 没有使用注释,但它没有意义。
最佳答案
所以我找到了一种方法来做到这一点。您需要实现 RequestStreamHandler,它为您提供可以使用的输入和输出流:
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
}
}
有了输入和输出流,您就可以独立于用于解析它的格式和框架。
关于java - 带有 jackson 注释的 AWS Lambda json 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41480472/