简体   繁体   English

数据模型类未按定义的顺序返回字段

[英]Data model class not returning fields in defined order

I am working on a JAX-RS RESTful service. 我正在使用JAX-RS RESTful服务。 My model class is returning the fields in alphabetical order. 我的模型类按字母顺序返回字段。 I want them returned in the order I added them in the class. 我希望它们按照我在课堂上添加它们的顺序返回。

Here is my model class: 这是我的模型课:

public class AuditRecord implements Serializable {
    private static final long serialVersionUID = 3682698298601640061L;

    private String application;
    private String objectName;
    private String objectType;
    private String system;
    private String createdBy;
    private String createdDate;
    private String createdTime;
    private String detectedDate;
    private String reconciledBy;
    private String reconciledDate;

    // removed GETTERS and SETTERS for brevity
}

I am instantiating the class and populating it in the order the fields are created: 我实例化该类并按照创建字段的顺序填充它:

while(rs.next()) {
    AuditRecord a = new AuditRecord();
    a.setApplication(rs.getString(1));      // PRAPPL
    a.setObjectName(rs.getString(2));       // PROBNM
    a.setObjectType(rs.getString(3));       // PROBAT
    a.setSystem(rs.getString(4));           // PRCRTS
    a.setCreatedBy(rs.getString(5));        // PRCRTU
    a.setCreatedDate(rs.getString(6));      // PRCDAT
    a.setCreatedTime(rs.getString(7));      // PRCTIM
    a.setDetectedDate(rs.getString(8));     // PRDDAT
    a.setReconciledBy(rs.getString(9));     // PRRECBY
    a.setReconciledDate(rs.getString(10));  // PRRECDT

    retVal.add(a);
}

Once the class object is populated, its added to a list and returned to my controller and sent back to the web page. 填充了类对象后,将其添加到列表中并返回到我的控制器并发送回网页。

My controller method is returning a JSON object. 我的控制器方法返回一个JSON对象。 I expect the fields to be in the order I created them, but when I populate my table, the objects are in alphabetical order. 我希望这些字段按创建顺序排列,但是当我填充表格时,对象按字母顺序排列。

I have not run into this in previous RESTful services I've worked on. 在我之前从事的RESTful服务中,我还没有遇到过这个问题。 How do I get the fields to be in the order they are defined? 如何使字段符合定义的顺序?

If I set a break point on the line adding the Audit Object to the ArrayList and view the object, the fields are listed in the wrong order 如果我在将Audit Object添加到ArrayList的行上设置了一个断点并查看了该对象,则字段以错误的顺序列出

在此处输入图片说明

To answer some of the question, rs is an instance on SQL Resultset. 为了回答某些问题,rs是SQL Resultset上的一个实例。 The query selects specific named fields in the order I need to display them. 该查询按显示顺序选择特定的命名字段。 I does not use select * from ... 我不使用select * from ...

Finally, the last piece is the controller method: 最后,最后一部分是控制器方法:

@GET
@Path("audits")
@Produces(MediaType.APPLICATION_JSON)
public Response getAuditData(@QueryParam("startDate") String startDate, @QueryParam("endDate") String endDate) {
    SoxService service = new SoxService();

    List<AuditRecord> data = new ArrayList<AuditRecord>();

    try {
        data = service.getAuditData(startDate, endDate);
    }catch(Exception e) {

    }

    return Response.ok(data, MediaType.APPLICATION_JSON).build();
}

So now, while putting these edits together, I realized I had not pulled the jersey bundle into my project. 所以现在,在将这些编辑放在一起的同时,我意识到我还没有将球衣捆绑到我的项目中。 This would be the only difference between this implementation of a RESTful service and others I have done. 这将是RESTful服务的实现与我已经完成的其他实现之间的唯一区别。 This one is being implemented in a different project. 这是在另一个项目中实现的。

Using Andrew Tobilko's suggestion, I added the @JsonPropertyOrder annotation and this has resolved my concern. 使用Andrew Tobilko的建议,我添加了@JsonPropertyOrder批注,这解决了我的担忧。

The question I have, then, is this is the first service I've done completely through annotation. 那么,我的问题是,这是我完全通过注释完成的第一项服务。 I've not added a servlet mapping to the web.xml. 我尚未将servlet映射添加到web.xml。 In my mind, it doesn't seem this diversion would have caused the issue, would it? 在我看来,这种转移似乎不会引起问题,不是吗?

The order you are using to populate an AuditRecord instance doesn't really matter. 您用来填充AuditRecord实例的顺序并不重要。 It matters how its fields get serialised and what order the underlying JSON marshaller has chosen. 它的字段如何序列化以及底层JSON编组器选择的顺序很重要。

If you are utilising Jackson, here's the solution: 如果您正在使用Jackson,请使用以下解决方案:

@JsonPropertyOrder({ 
    "application", 
    "objectName",
    ...
    "reconciledDate"
})
public class AuditRecord implements Serializable { ... }

You should arrange the field names in the way you want them to be serialised. 您应该按照希望序列化字段名称的方式进行排列。 Otherwise, Jackson will stick to the alphabetical order. 否则,杰克逊将坚持字母顺序。

If you don't use Jackson, you will probably need to write own serialiser. 如果您不使用杰克逊,则可能需要编写自己的序列化器。

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

相关问题 java中数据字段的初始化顺序 - Order of initialization of data fields in java 如何访问Android列表中定义的类字段? - how to access defined class fields in a list in android? 实现在接口中定义的克隆返回具体的类? - Implement clone defined in an Interface returning the concrete class? 将 Java 数据 Class 转换为前端的字段数组/元数据(类似于 Swagger 数据模型/模式)? - Convert Java Data Class to Array of Fields/Metadata for frontend (Similar to Swagger Data Model/Schema)? Class.getFields()返回的字段顺序 - Order of Fields returned by Class.getFields() 单例中类字段和方法的Java顺序 - Java order of class fields and methods in singleton Java反射:类字段和方法的顺序是否标准化? - Java reflection: Is the order of class fields and methods standardized? Java class 中的成员字段是否按声明顺序初始化? - Are the member fields in a Java class initialized in order of declaration? 如何使用Spring Data Mongo DB对类进行建模,以存储具有不同字段和类型长度的嵌套JSON文档 - How to Model class using Spring Data Mongo DB to store nested JSON document with varying length of fields and type Spring jpa 实体管理器 getResultList 从多个表返回数据以适应非模型类 - Spring jpa entity manager getResultList returning data from multiple tables to fit in to non model class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM