[英]List<object> null when deserializing json in Spring Boot controller
我在 Spring 引导应用程序中遇到了 controller 问题。 当我在 controller(URI/调用)上进行调用时,object 列表始终是 null。
{
"code": "TEST",
"name": "TEST NAME",
"groupe": "A1",
"list": [
{"type":"web", "link":"https://google.com/"},
{"type":"web", "link":"https://google2.com/"}
]
}
@PostMapping(value="/call")
public ResponseEntity<Void> ajouterEnvironnement(@RequestBody First first) {
first.getCode() // value : "TEST"
first.getList() // value : null
}
public class First {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String code;
private String name;
private String groupe;
@OneToMany(mappedBy = "first", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Second> list;
}
public class Second {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String type;
private String link;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "first_id", nullable = false)
private First first;
}
通过写这个问题,我能够找到一个解决方案,所以我分享它:
使用@JsonManagedReference
和@JsonBackReference
注释。
public class First {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String code;
private String name;
private String groupe;
@OneToMany(mappedBy = "first", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JsonManagedReference
private List<Second> listLiens;
}
public class Second {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String type;
private String link;
@ManyToOne(fetch = FetchType.EAGER)
@JsonBackReference
@JoinColumn(name = "first_id", nullable = false)
private First first;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.