简体   繁体   中英

Java type for AWS SNS message from SQS

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 ?

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

使用对象中的@NotificationMessage 注释(表示消息)作为监听器。

    @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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM