2

I have a AWS SQS queue subscribed to a SNS topic. Message I receive from SQS queue looks like this:

Message body: {
  "Type" : "Notification",
  "MessageId" : "6ffbe51a-5c00-51f8-a67e-b468ad721131",
  "TopicArn" : "arn:aws:sns:eu-central-1:447379608829:dev_com_pio_admin_package",
  "Message" : "CUSTOM_JSON_OBJECT",
  "Timestamp" : "2017-04-20T17:26:10.410Z",
  "SignatureVersion" : "1",
  "Signature" : "iLDcSwI5CJ.....==",
  "SigningCertURL" : "https://sns.eu-central-1.amazonaws.com/...............",
  "UnsubscribeURL" : "https://sns.eu-central-1.amazonaws.com/..............."
}

Is there a Java representing this kind of message in Spring Cloud AWS or in AWS java SDK?

Michal Foksa
  • 11,225
  • 9
  • 50
  • 68

3 Answers3

0

That is a json representation of the message. The easiest way to convert that to a java object is probably gson.

quodlibet
  • 452
  • 3
  • 8
0

Use the annotation @NotificationMessage in Object (represent message) for listener.

0
    @SqsListener("spring-boot-sqs")
public void getProductoFromSQS(String snsMessageJsonFormat) {
    Gson gson = new Gson();
    SNSMessage snsMessage = gson.fromJson(snsMessageJsonFormat, SNSMessage.class);
    Producto producto = gson.fromJson(snsMessage.getMessage(), Producto.class);
    itemservice.save(producto);
}

@Getter
@Setter
public class SNSMessage {
    private String Type;
    private String MessageId;
    private String TopicArn;
    private String Message;

}

how you can see you need to model Java Class SNSMessage

get the String Message and convert to appropriate Object with Gson. :)