Im trying to insert a list of phones with no success.
My Domain Client class
@Entity
public class Client {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Integir id;
private String name;
@JsonManagedReference
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(****)
private List<Phone> phones = new ArrayList<>();
**gets and sets**
public Client (String name){
this.name = name;
}
}
My Domain Phone class
@Entity
public class Phone {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Integir id;
private String number;
@JsonBackReference
@ManyToMany(mappedBy = "phones")
private List<Client> clients = ArrayList<>();
**gets and sets**
public Phone(String number) {
this.number = number;
}
}
I have a DTO ClientDTO
public class ClientDTO {
private String name;
private String number;
private List<Phone> numbers = ArryList<>();
**gets and sets**
}
My service ClientService hava this method:
public Client fromDTO (ClientDTO clientDTO) {
Client c = new Client(clientDTO.getName());
for(Phone p: clientDTO.getNumbers()) {
p.setNumber(x.getNumber);
p.getClients().addALL(Arrys.asList(c));
c.getPhones().addAll(Arrys.asList(p));
}
}
If i try to this in Postman:
{
"name" : "Teste",
"numbers":
[
{ "number" : "xxxxxx"},
{ "number" : "yyyyyyy"}
]
}
Shows Error 415 : Unsupported Media Type
If i change my ClientDTO to:
public class ClientDTO {
private String name;
private String number;
**gets and sets**
}
And my method in ClientService to:
public Client fromDTO (ClientDTO clientDTO) {
Client c = new Client(clientDTO.getName());
Phone p = new Phone(clientDTO.getPhone());
p.getClients().addAll(Arrys.asList(c));
c.getPhones().addAll(Arrays.asList(p));
}
And in my postman i send this:
{
"name" : "Teste",
"number": "xxxxx"
}
Works fine.
What Im doing wrong?