简体   繁体   English

使用spring框架将Java对象中的顺序保存到json

[英]Preserving order in Java object to json with spring framework

I created a Java Spring MVC web app which offers simple restful services. 我创建了一个Java Spring MVC Web应用程序,它提供简单的restful服务。 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. 我有使用Postman的json格式文件上传到服务器。 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.. 我希望格式与类中的对象顺序相同,例如topic,subscriptionReference,utcTime,orderm,message ..但是我得到了带有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. 附上我的spring配置和porm.xml以供参考。

rest.servlet.xml 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 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>
...

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 我建议你在代码中使用lowerCamelCase命名变量约定,因为它最受欢迎

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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