猿问

Json和数组序列化-Java Spring Boot

我试图插入没有成功的电话列表。


我的域客户端类


@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;

   }

}

我的域电话课程


@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;

    }  

}

我有一个DTO客户


public class ClientDTO {


private String name;

private String number;

private List<Phone> numbers = ArryList<>();

**gets and sets** 

}

我的服务ClientService具有此方法:


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));

  }

}

如果我在邮递员中尝试这样做:


{

 "name" : "Teste",

 "numbers": 

  [

   { "number" : "xxxxxx"},

   { "number" : "yyyyyyy"}  

  ]

}

显示错误415:不支持的媒体类型


如果我将ClientDTO更改为:


public class ClientDTO {


private String name;

private String number;

**gets and sets** 

}

而我在ClientService中的方法可以:


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));

}

然后在我的邮递员中发送:


{

  "name" : "Teste",

  "number": "xxxxx" 

}

工作良好。


我在做什么错?


慕码人8056858
浏览 217回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答