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

在 Spring Boot 控制器中反序列化 json 时 List<object> null

在 Spring Boot 控制器中反序列化 json 时 List<object> null

慕后森 2024-01-17 20:48:14
我在 Spring Boot 应用程序中遇到控制器问题。当我在控制器上进行调用(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;}
查看完整描述

1 回答

?
浮云间

TA贡献1829条经验 获得超4个赞

通过写这个问题,我找到了解决方案,所以我分享一下:

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

}


查看完整回答
反对 回复 2024-01-17
  • 1 回答
  • 0 关注
  • 35 浏览

添加回答

举报

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