我正在尝试通过 Gson 将我的 bean 转换为 json。我的 bean(日志)包括另一个 bean(日志)和对第一个 bean 的第二个 bean 引用。
@Entity
@Table(name = "t_logs")
public class Logs {
@Id
@Column(name = "executionid")
private String executionId;
@Column(name = "sentdate")
@Temporal(TemporalType.TIMESTAMP)
private Date sentDate;
@Column(name = "sent")
private boolean sent;
@OneToMany(mappedBy = "logs", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Log> logList;}
@Entity
@Table(name = "t_log")
public class Log {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;
@ManyToOne
@JoinColumn(name = "executionid")
private Logs logs;
@Column(name = "startdate")
@Temporal(TemporalType.TIMESTAMP)
private Date startDate;
@Column(name = "enddate")
@Temporal(TemporalType.TIMESTAMP)
private Date endDate;
}
因此,当我尝试转换 Json 格式时,会出现上述错误; java.lang.StackOverflowError
我认为这是一个循环,但我该如何解决,我不知道
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
return gson.toJson(logs, Logs.class);