Issue
I am showing some data to the user:
{
"id": 3,
"name": "AB:11",
"description": "AB:11 is an Imperial Black Barley Wine brewed with ginger, black raspberries and chipotle peppers. A 12.8% rollercoaster of ginger zestiness and chipotle smokiness, all bound together with dark berry tartness and the decadent residual body of a Black Barley Wine.",
"method": {
"id": 3,
"mash_temp": [
{
"id": 6,
"temp": {
"value": 68,
"unit": "celsius"
}
}
]
}
}
But, I want to show it without "method" I only need to show what is inside "mesh_temp" field,so it would be something like this:
"id": 3,
"name": "AB:11",
"description": "AB:11 is an Imperial Black Barley Wine brewed with ginger, black raspberries and chipotle peppers. A 12.8% rollercoaster of ginger zestiness and chipotle smokiness, all bound together with dark berry tartness and the decadent residual body of a Black Barley Wine.",
"mash_temp": [
{
"temp": {
"value": 68,
"unit": "celsius"
}
}
]
}
Also, like you can see in first example I don't want to show id of the mesh_temp
Those are my Entities:
- Beer: Beer class
- Method: Method class
- Mash Temp: MashTemp class
- Temp: Temp class
So just to summarize: I want to show only: id, name, description and mash_temp field (without id). Any advices how to do this?
Solution
The simplest way is to create a DTO that is used in the response on your API with exactly the attributes you want. This is in fact a suggested pattern exactly because of use cases similar to this one: when your internal model is different from the model you want to make available via your API.
Answered By - João Dias