Jackson and json array

0

All good people? I'm killing my head for a few days but without success.

I need to insert information into the DB that comes through JSON. It turns out that when an array comes, it does not want to insert.

Explain better:

In the domain, I have the client class:

@Entity
public class Cliente
private String nome;
@ManytoMany
private List<Telefone> telefones = new ArrayList<>();

public Cliente() {}

getters e setters 

And I have the telephone class:

@Entity
public class Telefone
private String numero;
@JsonIgnore
@ManytoMany
private List<Cliente> clientes = new ArrayList<>();

public Telefone () {}

getters e setters

I created a DTO Client class:

public class ClienteDTO
private String nome;
private String numero;

public ClienteDTO () {}

getters e setters

In the service I created the ClientService class which, in addition to some methods, has the following method:

public Cliente converteDoDTO(ClienteDTO clienteDTO) {

Cliente c = new Cliente(clienteDTO.getNome());
Telefone t = new Telefone(clienteDTO.getNumero());

c.getTelefones().addALL(Arrays.asList(t));
t.getClientes().addALL(Arrays.asList(c));

}

This way if I pass JSON this way:

{
  "nome" : "Teste",
  "telefone" : "123"
}

It will be included in the DB.

But I would like it to be as follows:

{
  "nome" : "Teste",
  "telefones" : 
 [
   {"telefone" : "123"},
   {"telefone" : "456"},
   {"telefone" : "789"}
 ]
}

Only your doing this, the "phones" comes empty. And the bank only enters the name. I searched and saw that I have to use ObjectMapper, something like this:

ObjectMapper mapper = new ObjectMapper();
List<Telefone> telefones = mapper.readValue(jsonInput, new TypeReference<List<telefone>>(){});

I happen to not know where to create this. If it is in the serivce, in the DTO, in the Domain. I'm very lost. Could anyone help? Thank you in advance.

    
asked by anonymous 27.06.2018 / 04:24

0 answers