【问题标题】:Preserving order in Java object to json with spring framework使用spring框架将Java对象中的顺序保留为json
【发布时间】:2016-02-07 04:00:49
【问题描述】:

我创建了一个 Java Spring MVC Web 应用程序,它提供了简单的 Restful 服务。 我有这样的对象类..

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;
    ...
}

控制器:

...
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);

    }
}

我有使用 Postman 上传到服务器的 json 格式文件。 上传功能正常。

{
 "topic":"Face",
 "subscriptionReference":"http:\\abc",
 "utcTime":"2016-10-20T19:00:00Z",
 "messages":[{
    "source":[{"name":"VideoSource","value":"[1]123.avi"},
              {"name":"AnalyticEngine","value":"FacialRecognition"}]        
  }]
}

我希望格式与类中的对象顺序相同 诸如主题、订阅参考、utcTime、orderm、message.. 但我得到了消息、subscriptionReference、utcTime、topic..的输出。

{
  "messages": [
    {

      "source": [
        {
          "name": "VideoSource",
          "value": "[1]123.avi"
        },
        {
          "name": "AnalyticEngine",
          "value": "FacialRecognition"
        }
      ]
    }
  ],
  "subscriptionReference": "http:\abc",
  "utcTime": 1476990000000,
  "topic": "Face"
}

我可以像类对象顺序一样有特定的顺序吗?

附上我的 spring 配置和 porm.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

...
    <!-- 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>
...

【问题讨论】:

    标签: java json spring rest spring-mvc


    【解决方案1】:

    你应该试试这个

    //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;
        ...
    }
    

    我建议您在代码中使用 lowerCamelCase 命名变量约定,因为它最流行

    【讨论】:

    • @hismart 没问题,记住小写字母(我在代码中添加了注释)
    • 是的。我使用小写字母并解决问题。如果其他人需要此解决方案,请记住小写字母。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多