JSON don't repeat some attributes and respective values

304 Views Asked by At

for all instances serialized, from the second occurrence onwards, of the same model class, the objects only have a part of the attributes and their respective values ​​that they should. This way the JSON file structure is not homogeneous and uniform as expected by the old part of the application (Frontend).

[
{
    "id": 1,
    "fornecedor": {
        "cnpj": "80000876000177",
        "nome": "ATACADÃO DIA-A-DIA"
    },
    "itens": [
        {
            "id": 2,
            "produto": {
                "gtin": "7891991010856",
                "nome": "CERVEJA BUDWEISER"
            },
            "quantidade": 3,
            "preco": 3.5,
            "total": 10.5
        }
    ]
},
{
    "id": 3,
    "fornecedor": {
        "cnpj": "19600347000101",
        "nome": "SUPERMERCADO BELLA-VIA"
    },
    "itens": [
        {
            "id": 4,
            "produto": {
                "gtin": "7891991010857",
                "nome": "CERVEJA HEINEKEN"
            },
            "quantidade": 4,
            "preco": 5.4,
            "total": 21.6
        }
    ]
},
{
    "id": 5,
    "fornecedor": "19600347000101",
    "itens": [
        {
            "id": 6,
            "produto": "7891991010856",
            "quantidade": 4,
            "preco": 3.2,
            "total": 12.8
        }
    ]
},
{
    "id": 7,
    "fornecedor": "80000876000177",
    "itens": [
        {
            "id": 8,
            "produto": "7891991010857",
            "quantidade": 5,
            "preco": 4.9,
            "total": 24.5
        }
    ]
}
]

In the JSON above, the instances with id:1 and id:7 have a field named fornecedor that is structurally different and that shouldn't be because the Frontend expects them to be the same. Other samples are id:2 and id:6 with the field produto and so on.

The JSON above is the serialization of the list of model class Pedido below:

@Data
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") 
public class Pedido {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonView(View.External.class)
private Long id;

@ManyToOne
@JoinColumn(name = "fornecedor_cnpj")
@JsonView(View.External.class)
private Fornecedor fornecedor;

@OneToMany(mappedBy = "pedido", fetch = FetchType.LAZY,
        cascade = CascadeType.ALL)
@JsonView(View.External.class)
private List<Item> itens;

public List<Item> getItens() {
    if(itens == null){
        itens = new ArrayList<>();
    }
    return itens;
}
}

The model class Fornecedor is:

@Data
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "cnpj")
public class Fornecedor {

@Id
@JsonView(View.External.class)
private String cnpj;

@JsonView(View.External.class)
private String nome;

@OneToMany(mappedBy = "fornecedor", fetch = FetchType.LAZY,
        cascade = CascadeType.ALL)
@JsonView(View.Internal.class)
private List<Pedido> pedidos;

@OneToMany(mappedBy = "fornecedor", fetch = FetchType.LAZY,
        cascade = CascadeType.ALL)
@JsonView(View.Internal.class)
private List<Disponibilidade> disponilidades;
}

And the model class Produto is:

@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "gtin")
@NoArgsConstructor
@Data
public class Produto {

@Id
@JsonView(View.External.class)
private String gtin;

@JsonView(View.External.class)
private String nome;

@OneToMany(mappedBy = "produto", fetch = FetchType.LAZY,
        cascade = CascadeType.ALL)
@JsonView(View.Internal.class)
private List<Disponibilidade> disponibilidades;

public Produto(String gtin) {
    this.gtin = gtin;
}
}

Question: The structure of JSON is the representation of expected behavior? If so, could someone tell me how do I make all instances from the first one to always have all attributes with their respective values ​​(as much as that means repeated attributes and values!).

Thanks!

1

There are 1 best solutions below

0
João Dias On BEST ANSWER

The JSON you are getting is a consequence of the usage of @JsonIdentityInfo that you or someone else may have added to solve an infinite recursion while serializing your Java objects to JSON (you can read more about this at https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion). This means that the only way to have the complete set of attributes in fornecedor and produto is to get rid of @JsonIdentityInfo on those classes. Keep in mind that this might create the infinite recursion problem mentioned above.

@Data
@Entity
public class Fornecedor {
    (...)
}
@Entity
@NoArgsConstructor
@Data
public class Produto {
    (...)
}