gpt4 book ai didi

java - 在 Spring Boot Controller 中反序列化 json 时 List null
转载 作者:行者123 更新时间:2023-12-01 22:12:27 28 4
gpt4 key购买 nike

我在 Spring Boot 应用程序中遇到 Controller 问题。当我在 Controller 上进行调用(URI/调用)时,对象列表始终为空。

{
"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注释。

More information here .

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

关于java - 在 Spring Boot Controller 中反序列化 json 时 List<object> null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58648762/

28 4 0