I created a Java Spring MVC web app which offers simple restful services. And I have the object class like this..
public class CreateEventWrapper {
    private String Topic;
    private String SubscriptionReference;
    private Date UtcTime;
    private List<Message> Messages;
    ...
}
public class Message {
    private List<Source> Source;
    ...
}
public class Source {
    private String Name;
    private String Value;
    ...
}
The controller:
...
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(produces = "application/json")
class EventController {
    @RequestMapping(value = "/event", method = RequestMethod.POST)
    public ResponseEntity<CreateEventWrapper> CreateEvent(@RequestBody CreateEventWrapper eventWrapper) {
    return new ResponseEntity<CreateEventWrapper>(eventWrapper, HttpStatus.OK);
    }
}
And I have the json format file using Postman to upload to the server. The upload function works fine.
{
 "topic":"Face",
 "subscriptionReference":"http:\\abc",
 "utcTime":"2016-10-20T19:00:00Z",
 "messages":[{
    "source":[{"name":"VideoSource","value":"[1]123.avi"},
              {"name":"AnalyticEngine","value":"FacialRecognition"}]        
  }]
}
I expect the format will be the same with the object order in the class something like topic, subscriptionReference, utcTime, orderm, message.. but I got the output with message, subscriptionReference, utcTime, topic..
{
  "messages": [
    {
      "source": [
        {
          "name": "VideoSource",
          "value": "[1]123.avi"
        },
        {
          "name": "AnalyticEngine",
          "value": "FacialRecognition"
        }
      ]
    }
  ],
  "subscriptionReference": "http:\abc",
  "utcTime": 1476990000000,
  "topic": "Face"
}
Can I have the specific order just like the class object order?
Attach with my spring config and porm.xml for reference.
rest.servlet.xml
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/task
                           http://www.springframework.org/schema/task/spring-task-3.0.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
    <mvc:annotation-driven />
    <mvc:default-servlet-handler/>
    <!-- Activates annotation-based bean configuration -->
    <context:annotation-config />
    <!-- Scans for application @Components to deploy -->
    <context:component-scan base-package="org.itri.ccma.paas.service.event.webservice" />
</beans>
porm.xml
...
    <!-- Json lib -->
    <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.4</version>
        <classifier>jdk15</classifier>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-hibernate4</artifactId>
        <version>2.3.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.6.1</version>
    </dependency>
    <!-- jackson -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.4.2</version>
    </dependency>
    <!-- Springframework -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>
...
                Yes, the order of elements in JSON arrays is preserved. From RFC 7159 -The JavaScript Object Notation (JSON) Data Interchange Format (emphasis mine): An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
The JSON standard defines objects as "an unordered collection of zero or more name/value pairs". As such, an implementation does not need to preserve any specific order of object keys.
Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. Spring automatically deserializes the JSON into a Java type, assuming an appropriate one is specified.
You shall try this
//lower cases since it affects the json output not the class field names
@JsonPropertyOrder({ "topic", "subscriptionReference", "utcTime", "messages" })
public class CreateEventWrapper {
    private String Topic;
    private String SubscriptionReference;
    private Date UtcTime;
    private List<Message> Messages;
    ...
}
And I suggest you to use lowerCamelCase naming variables convention in your code since its most popular
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With