为了账号安全,请及时绑定邮箱和手机立即绑定

Json和数组序列化-Java Spring Boot

Json和数组序列化-Java Spring Boot

慕码人8056858 2021-04-08 17:15:57
我试图插入没有成功的电话列表。我的域客户端类@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;   }}我的域电话课程@Entitypublic 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" }工作良好。我在做什么错?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 216 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信