SpringBoot中/employee/swipe/all接口返回空JSON对象问题求助
问题排查:/employee/swipe/all返回空JSON对象
核心原因是Swipe实体类缺少属性的getter方法。Spring Boot默认用Jackson做JSON序列化,序列化时必须通过getter方法读取对象属性值,而你的Swipe类只写了构造方法,没给id、employee、swipeInTime提供getter,导致Jackson拿不到任何属性数据,最终返回空JSON对象。
反观正常工作的Employee实体,它为id和name定义了getter方法,所以Jackson能正常序列化并返回数据。
解决办法
给Swipe实体补充所有属性的getter方法:
@Entity public class Swipe { @Id @GeneratedValue private Long id; @ManyToOne private Employee employee; private LocalDateTime swipeInTime; public Swipe(){} public Swipe(Employee employee) { this.employee = employee; this.swipeInTime = LocalDateTime.now(); } public Swipe(Employee employee, LocalDateTime localDateTime) { this.employee = employee; this.swipeInTime = localDateTime; } // 添加以下getter方法 public Long getId() { return id; } public Employee getEmployee() { return employee; } public LocalDateTime getSwipeInTime() { return swipeInTime; } }
添加完成后重启项目,调用/employee/swipe/all接口就能正常返回带属性值的JSON数据了。
内容的提问来源于stack exchange,提问作者Rahul Babu
相关产品推荐
相关产品推荐

