Java JsonB、JPA、REST、CDI,生成不完整的JSON
我正在尝试从数据库中获取Jsonb对象。当尝试从数据库获取所有课程时,我收到一个错误Java JsonB、JPA、REST、CDI,生成不完整的JSON,java,database,rest,jpa,jsonb,Java,Database,Rest,Jpa,Jsonb,我正在尝试从数据库中获取Jsonb对象。当尝试从数据库获取所有课程时,我收到一个错误 Schwerwiegend: Generating incomplete JSON Warnung: StandardWrapperValve[rest.ApplicationConfig]: Servlet.service() for servlet rest.ApplicationConfig threw exception java.lang.StackOverflowError 我猜这是因为我
Schwerwiegend: Generating incomplete JSON
Warnung: StandardWrapperValve[rest.ApplicationConfig]: Servlet.service() for servlet rest.ApplicationConfig threw exception
java.lang.StackOverflowError
我猜这是因为我的班级是如何建立的。我的课堂上有一系列问题,如下所示
@Entity
public class Lesson extends EntityWithIntId {
private String lessonName;
@OneToMany(mappedBy="lessonID",cascade = CascadeType.ALL)
private List<Question> questions = new ArrayList<>();
public Lesson() {
}
public Lesson(String lessonName) {
this.lessonName = lessonName;
}
public void addQuestion(Question question) {
this.questions.add(question);
}
// getter setter
}
有人知道如何解决这个问题吗?所以我通过删除实体中以get…开头的方法来解决这个问题。。。。并且不返回类变量。例如getLessonById()之类的。我猜JSON将它们视为类变量returner,并希望从中获取数据。此外,双向关系也会导致无休止的循环,因此您要么使用特定的注释来忽略这些循环,要么完全避免它们。您可以使用annotation@JsonbTransient(javax.json.bind.annotation.JsonbTransient)为了防止序列化,FILD会导致对象图中出现圆圈。为什么更改实体类会更改数据库?从技术上讲,数据库将保持不变。但是,当我想删除某些内容时,我遇到了错误。我再也不记得错误是什么了,但我相信这是因为我没有完全理解它。两个版本的
课程
都有cascade=cascade类型。所有
,两个版本的问题
都没有设置cascade类型。当前,我正在创建对象并用数据填充它们。最后,我只是将其保存下来,例如:Lesson testLesson=newlesson(“testLesson”);问题testQuestion=新问题(testLesson,“testQuestion”);testQuestion.addAnswer(“Test1”);testQuestion.addAnswer(“Test2”);添加问题(testQuestion);创建(testLesson)代码>但在更改的版本中,我必须在DB中创建课程,然后才能获取问题的ID,否则ID将为空。所以我必须改变我的整个程序。不管怎样,我的问题是我一直遇到这个错误:Fatal:生成不完整的JSON警告:StandardWrapperValve[de.hsos.kbse.util.JAXRSConfiguration]:Servlet.service()对于servlet de.hsos.kbse.util.jaxrConfiguration引发异常java.lang.IllegalArgumentException:参数数错误
@Entity
public class Question extends EntityWithIntId {
@ManyToOne
@JoinColumn(name = "lessonID")
private Lesson lessonID;
private String question;
@ElementCollection
@MapKeyColumn(name = "answer")
@Column(name = "solution")
@CollectionTable(name = "answers", joinColumns = @JoinColumn(name = "questionID"))
Map<String, Integer> answers = new LinkedHashMap<>();
public Question() {
}
public Question(Lesson lessonID) {
this.lessonID = lessonID;
}
public Question(String question) {
this.question = question;
}
public Question(Lesson lessonID, String question) {
this.lessonID = lessonID;
this.question = question;
}
public void addAnswer(String answer) {
this.answers.put(answer, 0);
}
// getter setter
}
@Entity
public class Lesson extends EntityWithIntId {
private String lessonName;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "lessonID")
private List<Question> questions = new ArrayList<>();
}
@Entity
public class Question extends EntityWithIntId {
private int lessonID;
private String question;
@ElementCollection
@MapKeyColumn(name = "answer")
@Column(name = "solution")
@CollectionTable(name = "answers", joinColumns = @JoinColumn(name = "questionID"))
Map<String, Integer> answers = new LinkedHashMap<>();
}
@GET
@Produces({MediaType.APPLICATION_JSON})
public Response getAllLesssons() {
if (super.findAll() != null) {
return Response.ok(super.findAll()).build();
} else {
String message = "Lessons not found!";
return Response.ok().type(MediaType.TEXT_PLAIN).entity(message).build();
}
}