问题描述
我有以下几点:
@Data
@Entity
@Table(name = "floor")
public class Floor {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "floor_id")
private Long id;
@JsonIgnoreProperties(value= {"floor","registrations"})
@OneToMany (mappedBy = "floor",cascade = CascadeType.ALL)
private Set<Slot> slots;
}
@Entity
@Table(name = "slot")
public class Slot {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "slot_id")
private Long id;
@ManyToOne
@JoinColumn(name = "floor_id")
private Floor floor;
}
@RestController
public class Controller {
@Autowired
FloorService floorService;
@GetMapping("/api/floors")
public List<Floor> getAllFloors() {
// calls repository.findAll()
return floorService.getAllFloors();
}
}
访问 API 端点时,我得到:
2021-03-06 06:52:30.038 WARN 699889 --- [tp1028811481-19] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: java.util.ArrayList[0]->com.veebzone.parking.model.Floor["slots"])]
通过使用 /api/floors 端点获取每个楼层的列表,我的目标是在其 JSON 元素中获取与每个楼层关联的插槽列表。
实现这一目标的最佳做法是什么:
- 我的方法是正确的,只是一些配置问题?
- 我应该改用服务返回的一些 DTO(将插槽列表作为子元素)并使用 JSONIgnore 作为插槽字段。
- 别的东西
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)