AWS Lambda json deserialization with jackson annotations

后端 未结 5 1653
生来不讨喜
生来不讨喜 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:06

    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.

提交回复
热议问题