0

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

enter image description here

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?

7
  • JSON does not guarantee ordering in a collection of key/value pairs. See: stackoverflow.com/questions/7198412/… Commented Dec 11, 2018 at 18:19
  • I'm not overly concerned with the JSON object. The class, itself, returns the fields in the wrong order. Commented Dec 11, 2018 at 18:21
  • Please clarify your problem. How are the fields returned? What is the return type? Is the returned object an array or list of strings? Any additional detail would be helpful. Commented Dec 11, 2018 at 18:24
  • 1
    What is this rs? If it's a SQL ResultSet, show your query. Commented Dec 11, 2018 at 18:33
  • 1
    @AndrewTobilko If it's a SQL query and the OP is using SELECT *, then the query doesn't constrain the column order. Commented Dec 11, 2018 at 18:56

1 Answer 1

2

The order you are using to populate an AuditRecord instance doesn't really matter. It matters how its fields get serialised and what order the underlying JSON marshaller has chosen.

If you are utilising Jackson, here's the solution:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.