I am using Java with Spring MVC and MyBatis to connect to the database. I have an account table and for some queries I need all the fields, but for others I need only some properties.
For example, to show the basic information of the account after consulting with MyBatis, some fields that I do not specify are returned as null
, false
or even 0
. Initially this was my ResponseBody:
{
"accountName": "Pagosonline.com",
"country": "CO",
"commercialName": "PayU - Latam",
"city": "Chiquinquirá",
"location": null,
"secureName": null,
"description": null,
"document": "8301097231",
"localOperation": false,
"address": null,
"acceptInternationalTransactions": false,
"balance": 0
}
Then I used the following annotation to avoid fields with null values:
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Account implements Serializable {...}
Currently my ResponseBody deletes the nulls like this:
{
"accountName": "Pagosonline.com",
"country": "CO",
"commercialName": "PayU - Latam",
"city": "Chiquinquirá",
"document": "8301097231",
"localOperation": false,
"acceptInternationalTransactions": false,
"balance": 0
}
But since I do not need the last 3 fields in the query, these are returned false
and 0
. An alternative is to create a Model only with the fields that I need. But first I want to know if there is another solution, maybe removing those fields while the ResponseBody is generated.
The expected response is:
{
"accountName": "Pagosonline.com",
"country": "CO",
"commercialName": "PayU - Latam",
"city": "Chiquinquirá",
"document": "8301097231"
}