AWS Lambda json deserialization with Jackson annotations

I call aws lambda with json body. So json fields have a different name from POJO fields. So what I did was to add @JsonProperty to the fields to tell Jackson what names are in json. But for some reason, it seems that he does not recognize them, and all fields are zero. If I pass json with the same field names as POJO, it works. Here is 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 } 

If i pass

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

all fields are null and with differId, userId, eventDateTime it serializes normally, except that it also does not recognize my own serializers / deserializers, but this is actually the same problem.

My conclusion is that for some reason aws jackson doesn't work with annotations, but that doesn't make sense.

+5
source share
3 answers

So, I found a way to do this. You need to implement RequestStreamHandler, which gives you input and output streams that 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 } } 

The presence of input and output streams makes you independent of the format and frameworks used for its analysis.

+2
source

create getter methods for the properties and put @JsonProperty in the getter methods.

0
source

It looks like you have version mismatch between annotation types and databind ( ObjectMapper ): both MUST be the same major version. In particular, Jackson 1.x annotations work with Jackson 1.x databind; and 2.x from 2.x.

The differences are visible through the Java package: Jackson 1.x uses org.codehaus.jackson , while Jackson 2.x uses com.fasterxml.jackson . Be sure to import the appropriate annotations for the ObjectMapper that you are using.

0
source

Source: https://habr.com/ru/post/1262339/


All Articles