how to return an unlimit fields in a response for a restfull web service

Viewed 43

Any help or hint would be greatly appreciated it!! I am currently using Java 8. I have a web service that takes an sql statement as a parameter. That sql could have many fields in the select statement or parameters for the where clause.

In my java return statement based on the sql queries I could have many fields in the return statement. How can I create a Java object that could have different number of fields based on the number of fields in the query select statement?

1 Answers

You can maybe use a HashMap for storing the query result If that is allowed
through this way, you can get the length of the total fields or do other operations that map provides.
below is the example code

public ResponseEntity<?> postData(some request) {

    Map<String, Object> data = someRepository.getDataByID(someId); 
    // data that you  get from sql query, note that you have to map it though 
    // in response type of sql query of whatever java tool you are using for getting the sql data
    System.out.println("The fields are: " + data.keySet()); 
    for(String fieldKey: data) {
         System.out.println("key is " + key);
         System.out.println("value is " + data.get(key));
    }
    ... your logic
    return new ResponseEntity(data, HttpStatus.OK);
}
Related