我已經省略了類中的所有其他屬性等。我在問題列表中沒有任何列表是序列化的。我已經添加了答案列表的一個例子。 JSON響應很好,只是「答案」爲空。爲什麼?當我將它打印到控制檯時,測驗對象完全處於初始狀態(是的,即使它是惰性加載,存儲庫層中的自定義代碼)。我也用調試器來檢查。JAXB不會序列化完整的對象圖
@XmlRootElement
public class Quiz {
private List<Question> questions;
public List<Question> getQuestions() {
return questions;
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
}
public class Question {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "QuestionId", nullable = false)
@XmlElementWrapper(name = "answers")
public List<Answer> getAnswers() {
return answers;
}
public void setAnswers(final List<Answer> answers) {
this.answers = answers;
}
}
@Entity
@Table(name = "Answer")
public class Answer {
}
編輯:
好像註釋掉的代碼在作祟。切換註釋會使代碼正常工作。
public void setAnswers(final List<Answer> answers) {
//this.answers = answers;
this.answers.clear();
for (Answer answer : answers) {
addAnswer(answer);
}
}
public void addAnswer(final Answer answer) {
if (!(answers.contains(answer))) {
checkThatOnlyOneAnswerMayBeCorrect(answer);
answers.add(answer);
}
}
爲什麼在二傳手的邏輯造成麻煩? – LuckyLuke
'addAnswer'做什麼? –
@BlaiseDoughan更新 – LuckyLuke