0
I have doubts about the improvements in json/object conversion between server (Rest api) and client (javascript framework). My question is in relation to objects referenced by the main entity, should I send only Ids or complete object? As an example (simple) I have the following relationship:
{
"id":2,
"nome":"SÃO GONÇALO",
"cep":"24030103",
"estado":{
"id":1,
"nome":"RIO DE JANEIRO",
"sigla":"RJ"
}
}
This structure represents the relationship between CITY and STATE. When registering a new city (POST) should I send the full "state" object or only its ID to the server? Example:
- Sending complete "state" object (POST):
{ "nome":"SÃO GONÇALO", "cep":"24030103", "estado":{ "id":1, "nome":"RIO DE JANEIRO", "sigla":"RJ" } }
- Sending only status ID (POST):
{ "nome":"SÃO GONÇALO", "cep":"24030103", "idEstado": 1 }
Which of these approaches should be followed?
Regarding queries (GET), should I return to the full status reference or just your ID? The return only ID approach has the "problem" of needing a new query (javascript) to be able to display to the user the state name.
I believe that passing only the ID in the POST is the best, not traffic unnecessary data. If it’s best to follow the ID line, which is the best practice to convert these Ids on the server to objects (need the complete object on the server to validate business before persisting)?
This was a simple example, I have models in my business where the number of references is quite large. Java work on server and angular on frontend.
It’s usually just the ID for reducing data traffic... so when you have tables only the ID is in them
– LeandroLuk