I am working on a JAX-RS RESTful service. 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. 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. 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
To answer some of the question, rs is an instance on SQL Resultset. The query selects specific named fields in the order I need to display them. I does not use 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. 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.
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. In my mind, it doesn't seem this diversion would have caused the issue, would it?