无法将 House 实体中的公寓显示为整个列表并按 ID 单独显示。当我转到该页面时,它会给出一个错误,即无限循环
Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: task.homerent.model.House["city"]->task.homerent.model.City$HibernateProxy$ikPQOTJQ["house"])
链接到代码
这是控制器的样子:
HouseRestController
@GetMapping("/{id}")
@PreAuthorize("hasAuthority('user:read')")
public House userPostInfo(@PathVariable(value = "id") Long id) {
Optional<House> house = houseRepository.findById(id);
List<House> res = new ArrayList<>();
house.ifPresent(res::add);
return res.stream().filter(houses -> houses.getId().equals(id))
.findFirst()
.orElse(null);
}
该错误表明您对 serialization 有无限依赖
house->city->house
。您需要像这样解决这种依赖关系: