punkish
punkish

Reputation: 15268

specifying what data to return from a REST query

what is the preferred way to specify what columns to return for a resource?

a resource is a noun, so when I say, GET employees, I can specify query parameters to get a limited set of employees. But what about the info on each employee? If the employees table has 12 columns, but I want only three, what is the best way to specify them? Or, do I treat them as different resources?

GET employees(all columns)
GET employees(name, age)
GET employees(id, salary)

I have seen suggestions such as (note to overzealous editors: fictitious example below, please don't obfuscate it with markdown syntax)

http://path/to/server/employees/?q=queryparams&cols=col1,col5,co7

but that seems to be mixing the data to return with the query string. Should work but seems inelegant.

Upvotes: 0

Views: 96

Answers (2)

Mark Shevchenko
Mark Shevchenko

Reputation: 8207

Usually REST results should contain all columns except big or complex properties.

  • GET /employees returns a list of employees (possibly paged);
  • GET /employees/100 returns the employee with all columns of primitive types;
  • GET /employees/100/photo returns big binary property photo;

In generally, remote services should return large objects due to network latency.

According to the standard JSON API you can include related objects in the result with the include parameter:

GET /employees/100?include=manager,salary

Upvotes: 2

Dhananjay Waghela
Dhananjay Waghela

Reputation: 178

You want to use QueryMap in Retrofit rest API

For Example

In API Service

 @GET("/employees")
    Call<List<Employees>> getEmployees(
        @QueryMap Map<String, String> options
    );

In Activity

private void getEmployees() {  
    Map<String, String> data = new HashMap<>();
    data.put("q", "queryparams");
    data.put("cols", "col1,col5,co7");

    // simplified call to request the news with already initialized service
    Call<List<Employees>> call = Service.getEmployees(data);
    call.enqueue(…);
}

Fore More Details Please Visit Retrofit Docs : Retrofit-Rest

Upvotes: 0

Related Questions