siddharth5
siddharth5

Reputation: 11

How to sort REST API response in the desired fashion?

{
  "data": {
    "uCPE PostStaging Completed": false,
    "Order Submitted": true,
    "uCPE PreStaging Completed": false,
    "Poststaging device deploy success": true,
    "uCPE Activated": false
  },
  "status": "SUCCESS"
}

Above mentioned is my Response Body. This keeps changing when a different input is provided. But I want the response only in the following Order. The KEY should be in the below order, however the values for each key will be changing.

“Order Submitted”:”true”, 
"Poststaging device deploy success" : “true”,
"uCPE PreStaging Completed": “ true”,
“uCPE PostStaging Completed" : “false”,
“Order Completed”:”false”,
"uCPE Activated":"false"

Upvotes: 0

Views: 118

Answers (2)

SMaZ
SMaZ

Reputation: 2655

As @Alan Sereb and @Roman Vottner suggested, @JsonPropertyOrder is best option if you are using your pojo class as response.

However, If you are passing Collection classes object directly to your parser and want to maintain the order then you need to use LinkedXXX or ArrayList for list interface. In your case as your are using Map<List>, you should use LinkedHashMap implementation.

This has very good explanations of Collection framework.

Upvotes: 1

Ilya Sereb
Ilya Sereb

Reputation: 2571

If you are using Jackson, look into @JsonPropertyOrder.

In your case it would be something like this:

@JsonPropertyOrder({ 
    "orderSubmitted", 
    "deploySuccess", 
    "preStagingCompleted", 
    "postStaingCompleted", 
    "orderCompleted", 
    "ucpeActivated"
})

Upvotes: 1

Related Questions