Java delete some properties in the ResponseBody

1

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"
}
    
asked by AndreFontaine 27.07.2016 в 18:16
source

2 answers

1

To avoid creating a DTO, you can stop using the primitive data types in your entities and use the wrappers instead:

public class Account {
    //los otros campos...
    private Boolean localOperation; //Boolean en lugar de boolean
    private Boolean acceptInternationalTransactions;
    private Double balance; //te recomendaría BigDecimal en lugar de Double
    //resto de la clase...
}

In this way, the default values of these fields will no longer be false or 0 but null since they are objects. If you worry about the "overhead" in memory of this change, I'll tell you that the truth is not very big unless you work with large amounts of data stored in memory (I imagine 100k of these elements in memory at the same time, at least ).

    
answered by 27.07.2016 / 18:21
source
2

The most comfortable thing if you are sure that those fields are never going to navigate is to use @JsonIgnore on the getter that you do not want to be serialized.

As of version 2.6 of Jackson, com.fasterxml.jackson.annotation.JsonProperty can be used on the property that is not serialized.

For example:

@JsonProperty(access = Access.WRITE_ONLY)

private String propertyXXX;

The javadoc documentation indicates about this type of access:

/**
 * Access setting that means that the property may only be written (set)
 * for deserialization,
 * but will not be read (get) on serialization, that is, the value of the  property
 * is not included in serialization.
 */
  WRITE_ONLY
    
answered by 15.10.2016 в 21:33