因此,我们尝试设置数据库已多次遇到此问题。基本上,我们有一个联赛,其中会有多个比赛。每当我们尝试打印http://localhost:8080/api/leagues时,页面就会填充无限的联赛列表,然后出现此错误。
“无法呈现请求[/ api / leagues]和异常[无法编写JSON:无限递归(StackOverflowError)的错误页面;嵌套的异常是com.fasterxml.jackson.databind.JsonMappingException:无限递归(StackOverflowError)(通过参考链: org.hibernate.collection.internal.PersistentBag [0]-> com.dontfeed.Dont.Feed.model.League [“ teams]-> org.hibernate.collection.internal.PersistentBag [0]-> com.dontfeed。 Dont.Feed.model.Team [“联盟”]-“
有谁对我们如何解决这个问题有任何想法?
@JsonIgnoreProperties在那里,因为我试图进行故障排除。我可以拿出来。
import com.dontfeed.Dont.Feed.model.enumerator.LeagueFormat;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.List;
@Data
@Entity
@NoArgsConstructor
@Table(name = "leagues")
public class League {
@Id
@NotNull
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private long id;
private LocalDate dateCreated;
private int duration;
private LocalDate endDate;
@Enumerated(EnumType.STRING)
private LeagueFormat format;
private String logo;
private String matchFrequency;
private int maxTeams;
@Column(unique = true)
private String name;
private String description;
private LocalDate startDate;
private String passcode;
// Relationships
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@OneToOne(fetch = FetchType.LAZY)
private Game game;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@OneToOne(fetch = FetchType.LAZY)
private Tournament tournament;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@OneToMany(fetch = FetchType.LAZY)
private List<Match> matches;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToMany(mappedBy = "leagues", fetch = FetchType.LAZY)
private List<Team> teams;
@Override
public String toString() {
return "League{" +
"id=" + id +
", dateCreated=" + dateCreated +
", duration=" + duration +
", endDate=" + endDate +
", format=" + format +
", logo='" + logo + '\'' +
", matchFrequency='" + matchFrequency + '\'' +
", maxTeams=" + maxTeams +
", name='" + name + '\'' +
", startDate=" + startDate +
", description= " + description +
'}';
}
}
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.List;
@Data
@Entity
@NoArgsConstructor
@Table(name = "matches")
public class Match {
@Id
@NotNull
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private long id;
private float duration;
private LocalDate matchDate;
private long matchId;
private String score;
// Relationships
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
private Game game;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
private League league;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
private Team team_a;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
private Team team_b;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
private Team victor;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
private Tournament tournament;
@Override
public String toString() {
return "Match{" +
"id=" + id +
", duration=" + duration +
", matchDate=" + matchDate +
", matchId=" + matchId +
", score='" + score + '\'' +
", victor=" + victor +
'}';
}
}
import com.dontfeed.Dont.Feed.model.League;
import com.dontfeed.Dont.Feed.service.LeagueService;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@AllArgsConstructor
@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping(value = LeagueController.PATH, produces = MediaType.APPLICATION_JSON_VALUE)
public class LeagueController {
static final String PATH = "/api/leagues";
private final LeagueService leagueService;
@GetMapping
public ResponseEntity<?> getLeagues() {
System.out.println("Test");
if (leagueService.findAllLeagues() == null) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body("No results found");
}
return ResponseEntity
.status(HttpStatus.OK)
.body(leagueService.findAllLeagues());
}
最佳答案
从错误看来,您已将Team
内的League
和League
内的Team
映射了。
它开始转换为JSON的周期。
将@JsonIgnore
与League
内的Team
一起使用。