我想通過Gson將我的bean轉換爲json。我的bean(Logs)包含另一個bean(Log)和第二個bean引用。如何將bean與參考bean轉換爲java中的json?
@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);
你可以建議一個註釋來忽略它嗎? –
請看看這裏的選項,排除在Gson中的字段http://howtoprogram.xyz/2016/10/16/ignore-or-exclude-field-in-gson/ –
謝謝@Selvakumar,但我需要的字段進入日誌bean(for executionId) –