0

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?

1 Answer 1

1

Add an empty(no-args) constructor to both Client and Phone entity classes

Client Entity 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 (){}
   public Client (String name){
    this.name = name;
   }
}

Phone Entity 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() {}
    public Phone(String number) {
      this.number = number;
    }  
}

You can't serialize an object if it has no Serializable super-class and has no no-arg constructor. Since You used Phone entity class in ClientDTO and it has no no-arg constructor serialization process fails

Sign up to request clarification or add additional context in comments.

1 Comment

I included the default constructor but the error persists. So i included the annotation @JsonIgnore on ClientDTO -> List<Phone> and the error ends. But no phone is included. And now? what im doing wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.