AWS Lambda json deserialization with jackson annotations

后端 未结 5 1698
生来不讨喜
生来不讨喜 2021-01-07 22:43

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

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-07 23:26

    I had this same issue and needed MyCustomClass to be taken in and out of the Lambda Function correctly so that it can be passed through my State Machine in the Step Function without any hiccups.

    Building off what Hristo Angelov posted, I was able to get a solution that worked for me and I'm posting it hoping that it will help others that were stuck like I was:

    import java.io.InputStream;
    import java.io.OutputStream;
    
    import com.amazonaws.services.lambda.runtime.Context;
    import com.amazonaws.services.lambda.runtime.LambdaLogger;
    import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
    
    public class StaticSearchPagingLambdaFunctionHandler implements RequestStreamHandler {
    
        LambdaLogger logger = null;
        MyCustomClass myCustomClass = null;
    
        // Register the JavaTimeModule for LocalDate conversion
        ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
    
        @Override
        public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) {    
    
                myCustomClass = objectMapper.readValue(inputStream, MyCustomClass .class);
    
                // ...
                // Do stuff with myCustomClass
                // ...
    
                objectMapper.writeValue(outputStream, myCustomClass);
        }
    }
    

    Even though the JSON string will print out differently with the ObjectMapper writing to the OutPutStream, when the next lambda function takes it in while going through the Step Function, it will still get converted to LocalDate correctly.

    Make sure that in MyCustomClass your toString() method prints correctly. My toString() method looks like this:

    import java.time.LocalDate;
    
    import org.json.JSONObject;
    
    public class SimpleSearch {
    
        private LocalDate startDate;
        private LocalDate endDate;
    
        // ...
        // Getters and Setters for the LocalDate variables
        // ...
    
        @Override
        public String toString() {
            return new JSONObject(this).toString();
        }
    
        public SimpleSearch() {}
    }
    

    then your JSON printouts will always look like this when it gets sent to the lambda and not that other crazy Jackson format:

    {
        "startDate": "2018-11-01",
        "endDate": "2018-11-16"
    }
    

    Some of the Maven dependencies I used:

    
        org.json
        json
        20180813
    
    
    
        com.fasterxml.jackson.datatype
        jackson-datatype-jsr310
        2.9.7
    
    

    Hopefully AWS fixes the Jackson conversions to be reciprocal, to and from JSON, so that we wouldn't have to resort to these custom conversions anymore.

提交回复
热议问题