0

I'm developing a lambda function for aws using Spring Cloud Function. The function is triggered by a SQSEvent. The problem is that the json received can't be parsed in a SQSEvent instance because the fields names don't match the keys inside the json.

This is my function:

public Function<SQSEvent, String> func() {

        return event -> {

            //covert the received json message in to a SQSEvent
            log.info("Parsed event: {}", event);
            DynamoDbService dynamoDbService = new DynamoDbService();

            String stringBody = event.getRecords().get(0).getBody();
            Gson gson = new Gson();
            UpdateCommunicationsMessage updateCommunicationsEvent = gson.fromJson(stringBody, UpdateCommunicationsMessage.class);
            log.info("Updated item: {}", updateCommunicationsEvent);
            try {
                dynamoDbService.updateItem(updateCommunicationsEvent);
            } catch (Exception e) {
                log.error("error", e);
                return "KO";
            }
            return "OK";
        };
    }

The event I'm trying to send is like this:

{
  "Records": [
    {
      "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
      "receiptHandle": "MessageReceiptHandle",
      "body": "{\n                \n\t\t\t\t\t\"id\": \"aaa\",\n\t\t\t\t\t\"senderId\": \"aaa\",\n\t\t\t\t\t\"senderSystem\": \"aaa\",\n\t\t\t\t\t\"ndg\": \"aaa\",\n\t\t\t\t\t\"status\": \"aaa\",\n\t\t\t\t\t\"description\": \"aaa\",\n\t\t\t\t\t\"date\": \"aaa\"\n\t\t\t\t\n      }",
      "attributes": {
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "1523232000000",
        "SenderId": "123456789012",
        "ApproximateFirstReceiveTimestamp": "1523232000001"
      },
      "messageAttributes": {},
      "md5OfBody": "{{{md5_of_body}}}",
      "eventSource": "aws:sqs",
      "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:registro-comunicazioni-update-queue-dev",
      "awsRegion": "us-east-1"
    }
  ]
}

The exception is thrown because the name of the keys "Records" and "eventSourceARN" do not match the names of the fields in the SQSEvent class.

Is there a way to solve this issue?

I have tried reading the json event as a String and then converting it in a SQSEvent object but with no luck.

0 Answers0