Json created incomplete

Asked

Viewed 201 times

1

Next people had a loop problem in my project when creating Json the application looped. I was able to solve the loop problem with the @Jsonmanagedreference, @Jsonbackreference annotation. However, an incomplete Json is being created following:

[{"clienteId":3,"clienteNome":"Teste","clienteRua":"Nestor Barbosa","clienteNumero":890,"clienteComplemento":null,"clienteGarrafaos":[{"id":3,"cliente":3,"garrafao":{"garrafaoId":3,"garrafaoNome":"Cristal"},"quantidade":0}]},

{"clienteId":5,"clienteNome":"Natanael","clienteRua":"Nestor Barbosa","clienteNumero":890,"clienteComplemento":null,"clienteGarrafaos":[{"id":6,"cliente":5,"garrafao":3,"quantidade":3},{"id":5,"cliente":5,"garrafao":{"garrafaoId":5,"garrafaoNome":"Pet"},"quantidade":3}]}]

If you look at the array of the second object in the first element you will see the following :

customers":[{"id":6,"client":5,"garrafao":3,"quantity":3}

Comparing with the first object or even with the second element of the array of the second object it came complete.

How do I get everything complete? I’ll leave the github link with the project in the name branch Stackoverflow.

---------------- Edit --------------------------

I did some tests here and I realized that if the first Customer owns the garrafao in the second Customer he does not only carry the garrafao name his id, that is, the garrafaOne and garrafaoId attributes are not created in Json. Follows:

[{"clienteId":3,"clienteNome":"Teste","clienteRua":"Nestor Barbosa","clienteNumero":890,"clienteComplemento":null,"clienteGarrafaos":[{"id":3,"garrafao":{"garrafaoId":3,"garrafaoNome":"Cristal"},"quantidade":0},{"id":7,"garrafao":{"garrafaoId":5,"garrafaoNome":"Pet"},"quantidade":3}]},

{"clienteId":5,"clienteNome":"Natanael","clienteRua":"Nestor Barbosa","clienteNumero":890,"clienteComplemento":null,"clienteGarrafaos":[{"id":6,"garrafao":3,"quantidade":3},{"id":5,"garrafao":5,"quantidade":3}]}]

2 answers

0


I managed to solve the problem by making an ODT class for each model and mapping the entity to the DTO. Thank you all.

0

Maybe we need to adjust the notes, the @JsonManagedReference indicates that the property must be serialized, while the @JsonBackReference serves to omit the serialization property. Looking at your code, in the Client class there is the annotation @JSONManagedReference for List<ClienteGarrafao>, but in the clienteGarrafao class no notes were added to guide Jackson in serialization.

This link from Baeldung explains about the annotations: link.

You can try modifying your Clientegarrafao class:

public class ClienteGarrafao {
    @JsonBackReference
    private Cliente cliente;

    @JsonManagedReference
    private Garrafao garrafao;
}
  • Look I tried to do the Clientegarrafao class thing but it didn’t work out the same way, but it still paid off!

Browser other questions tagged

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