Could not write JSON: Infinite recursion (StackOverflowError); nested exception spring boot

前端 未结 7 1289
时光说笑
时光说笑 2020-12-08 23:18

This is my District Controller, when I try to fetch data after saving I get the error, even when I try get object form getDistrict(Long id) the same strikes ple

7条回答
  •  囚心锁ツ
    2020-12-08 23:51

    That's because for Statemaster in json the set of Districtmaster's is put. And each Districtmaster has the Statemaster in itself, so it's also put into the json. So that you get the infinite recursion

        @JsonIgnore
        @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, 
        mappedBy="statemaster")
        public Set getDistrictmasters() {
            return this.districtmasters;
        }
    

    Adding @JsonIgnore annotation on Set will prevent that recursion. You can put the @JsonIgnore at public Statemaster getStatemaster() either.

提交回复
热议问题