Spring Boot - Changing object property names when returning JSON from the API

Asked

Viewed 1,565 times

0

Good night to you guys! I have a Rest api created with spring boot + Hibernate and all that is right!

Let’s imagine the following scenario: The data engineer created the database as follows

TB_PESSOA

no_nome
nu_cpf
sg_uf
dt_aniversario

How would you do to "REMAPEAR" (I don’t know how to name this xD) for when the resource API is exposed to natural display....

nome
cpf
uf
aniversario

ps: I don’t know if it’s possible to "map". if someone has an idea =) ps: will consume with React, also would like to send in the same pattern (name,Cpf,Uf...)

1 answer

1

It is possible to change the properties at the time of serialization of the object.

What I think is important is to point out that this transformation is more related to your class than to the data model, as you exemplified.

As you did not give details of its implementation, I will explain using the class Pessoa as an example:

import java.util.Date;

import lombok.Data;

@Data
public class Pessoa {    
    private String no_nome;
    private String nu_cpf;
    private String sg_uf;
    private Date dt_aniversario;   
}

Some considerations:

  1. Spring Boot by default can use Jackson, GSON or JSON-B for object serialization/deserialization, using its auto configuration capabilities
  2. Each framework has its features and annotations to customize the response payload, the ideal is to check the documentation
  3. I will use the framework as an example Jackson

By default Jackson uses the class property name for serialization, so the person class will result in this JSON:

{
    "no_nome": "NULLPTR",
    "nu_cpf": "00000000191",
    "sg_uf": "PR",
    "dt_aniversario": "1994-01-01"
}

To change this behavior, we use the serialization framework annotations to customize the properties as below:

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Data;

@Data
public class Pessoa {
    @JsonProperty("nome")
    private String no_nome;
    @JsonProperty("cpf")
    private String nu_cpf;
    @JsonProperty("uf")
    private String sg_uf;
    @JsonProperty("aniversario")
    private Date dt_aniversario;
}

In that situation the @JsonProperty is specifying the name the field should take when serialized, resulting in the following:

{
    "nome": "NULLPTR",
    "cpf": "00000000191",
    "uf": "PR",
    "aniversario": "1994-01-01"
}

It is important to note that annotations leave code increasingly loaded and behind more complexity, it is common to adopt Dtos for API returns and not to use directly from the database entity (where I have already discussed about this here).

But, because they are at different times, it is possible to use a JPA entity as an example below:

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Data;

@Data
@Entity
@Table(name = "TB_PESSOA")
public class Pessoa {

    @Id
    @JsonIgnore
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @Column(name = "no_nome")
    @JsonProperty("nome")
    private String algumNome;

    @Column(name = "nu_cpf")
    @JsonProperty("cpf")
    private String algumCpf;

    @Column(name = "sg_uf")
    @JsonProperty("uf")
    private String algumUf;

    @Column(name = "dt_aniversario")
    @JsonProperty("aniversario")
    private Date umaDataDeAniversario;
}

During the serialization this class will produce the expected result, however, note that I have already had to introduce a @JsonIgnore not to let the database id appear on the payload. This brings serious problems when your model changes, and your API’s response payload should not change.

Links:

Docs Spring Boot - Serialization/Deserialization

Notes Jackson

Entities vs Dtos

  • Thank you for your reply my friend! It was great learning! I had already researched on DTO! now I know I’m on the right track.!

  • Great @Kasio does not forget to accept the answer :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.