Configure Objectmapper used by Java AWS Lambda

Stephen Gibson :

I am trying to develop an AWS Lambda function that is triggered by events from SQS.

I am using the spring-cloud-function-adapter-aws (version 1.0.0.RELEASE) and in specifically a SpringBootRequestHandler.

However, the ObjectMapper that is being used is case-sensitive and therefore failing to successful convert the Json coming from SQS.

SQS publishes the following Json and it is the Records field in particular that I'm having the problem with.

    {
  "Records": [
    {
      "body": "Hello from SQS!",
      "receiptHandle": "MessageReceiptHandle",
      "md5OfBody": "7b270e59b47ff90a553787216d55d91d",
      "eventSourceARN": "arn:aws:sqs:eu-west-1:123456789012:MyQueue",
      "eventSource": "aws:sqs",
      "awsRegion": "eu-west-1",
      "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
      "attributes": {
        "ApproximateFirstReceiveTimestamp": "1523232000001",
        "SenderId": "123456789012",
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "1523232000000"
      },
      "messageAttributes": {}
    }
  ]
}

I have tried the suggestions in this question, but to no avail. Configuring ObjectMapper in Spring

In my POJO, I've also added the below annotation but it isn't working either whilst it would outside of Lambda.

@JsonProperty("Records")
private List<SqsRecord> Records;

Any help would be much appreciated.

My Lambda handler is defined as:

public class SqsEventHandler extends SpringBootRequestHandler<SqsEvent, String> {}

The POJO defined as:

public class SqsEvent {

@JsonProperty("Records")
private List<SqsRecord> records;

@Data
public class SqsRecord {
    private String body;
    private String receiptHandle;
    private String md5OfBody;
    private String eventSourceARN;
    private String eventSource;
    private String awsRegion;
    private String messageId;
}

}

I expect the Json from the sample message to be able to be read in by the ObjectMapper, but the field "records" is null.

Stephen Gibson :

I got this issue solved in a more simple manner.

Referencing https://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-stream.html and in specific

if Lambda's serialization approach does not meet your needs, you can use the byte stream implementation

I am now using the SpringBootStreamHandler directly and I have created an ObjectMapper instance with my required configuration options in my Spring Configuration class as:

@Bean
public ObjectMapper objectMapper() {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return mapper;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=80345&siteId=1