Json created incomplete

1

Next staff was experiencing a loop problem in my project when creating the Json application looping. I was able to solve the loop problem with the annotation @JsonManagedReference, @JsonBackReference. But an Incomplete Json is being created:

[{"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:

  

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

How to make everything come complete? I'll leave the github link with the project in the StackOverflow branch.

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

I made some tests here and realized that if the first Client has the bottle in the second Client it does not have the bottle name only the id of it, that is, the bottle attributesName and bottleId is not created in Json. Following:

[{"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}]}]
    
asked by anonymous 26.09.2017 / 23:56

2 answers

0

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

    
09.10.2017 / 23:07
0

It may be necessary to adjust annotations, @JsonManagedReference indicates that the property should be serialized, whereas @JsonBackReference is used to omit the serialization property. Looking at your code, in the Client class there is the annotation @JSONManagedReference to List<ClienteGarrafao> , but in the client class, the annotations were not added to guide Jackson in serialization.

This Baeldung link explains the annotations: link .

You can try to modify your ClientGroup class:

public class ClienteGarrafao {
    @JsonBackReference
    private Cliente cliente;

    @JsonManagedReference
    private Garrafao garrafao;
}
    
27.09.2017 / 01:08