简体   繁体   中英

SQS - Sending payload with JSONObject push empty objects

I was testing different payload structures accepted by AWS SDK (org.springframework.cloud:spring-cloud-starter-aws-messaging:2.2.6.RELEASE). I am sending the message using convertAndSend function provided by QueueMessagingTemplate. I am able to send it successfully with a string payload or a custom java object. However, when I convert my custom java object to a JSONObject, and push the JSONObject to SQS, it seems the the messageBody being pushed is {empty:true}. When I send it with jsonObject.toString(), it works well though. I am confused on why convertAndSend works for custom java class/object but not for JSONObject type.

Below is a sample code on how I am doing the JSON conversion:

public JSONObject toJson() throws Exception {
    JSONObject json = new JSONObject();
    json.put("payload", this.payload);
    json.put("id", this.taskId);
    return json;
}

SQS only allows message content that is JSON formatted string. JSONObject is a Java object. To get it to a JSON formatted string your method needs to look like this:

public String toJson() throws Exception {
    JSONObject json = new JSONObject();
    json.put("payload", this.payload);
    json.put("id", this.taskId);
    return json.toString();
}

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