我试图从这两个实体类返回json。
Questions.java
@Entity
public class Questions {
@Id
@Column(name = "id")
private int id;
@Column(name = "question")
private String question;
@Column(name = "type")
private String type;
@Column(name = "description")
private String description;
@Column(name = "param")
private String param;
@Column(name = "maxlength")
private int maxlength;
@Column(name = "dependency")
private String dependency;
@OneToMany(mappedBy = "questions",targetEntity = Answers.class, cascade = CascadeType.ALL)
private Set<Answers> answers = new HashSet<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
public int getMaxlength() {
return maxlength;
}
public void setMaxlength(int maxlength) {
this.maxlength = maxlength;
}
public String getDependency() {
return dependency;
}
public Set<Answers> getAnswers() {
return answers;
}
public void setAnswers(Set<Answers> answers) {
this.answers = new HashSet<>(answers);
for(Answers answers1:answers){
answers1.setQuestions(this);
}
}
public void setDependency(String dependency) {
this.dependency = dependency;
}
}
Answers.java
@Entity
public class Answers {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "ans_label")
private String ans_label;
@Column(name = "ans_value")
private int ans_value;
@Column(name = "ans_weightage")
private int ans_weightage;
@Column(name = "is_default")
private int is_default;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "ques_id", nullable = false)
private Questions questions;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAns_label() {
return ans_label;
}
public void setAns_label(String ans_label) {
this.ans_label = ans_label;
}
public int getAns_value() {
return ans_value;
}
public void setAns_value(int ans_value) {
this.ans_value = ans_value;
}
public int getAns_weightage() {
return ans_weightage;
}
public void setAns_weightage(int ans_weightage) {
this.ans_weightage = ans_weightage;
}
public int getIs_default() {
return is_default;
}
public void setIs_default(int is_default) {
this.is_default = is_default;
}
public Questions getQuestions() {
return questions;
}
public void setQuestions(Questions questions) {
this.questions = questions;
}
}
我的控制器看起来像这样。
SaveApiController
@RequestMapping("/getData")
public @ResponseBody List<Questions> getData(){
List<Questions> questionss=saveApiServices.getQuestions();
return questionss;
}
我目前得到的json结果有很多重复值。
[{"id":1,"question":"१. व्यक्तिको पुरा नाम थर?", "type":"input_edittext",
"description":"","param":"smalltext","maxlength":20,"dependency":"",
"answers":
[{"id":0,"ans_label":"मुली","ans_value":1,"ans_weightage":0,"is_default":0,
"questions":{"id":1,"question":"१. व्यक्तिको पुरा नाम थर?",
"type":"input_edittext","description":"","param":"smalltext","maxlength":20
,"dependency":"","answers":[{"id":0,"ans_label":"मुली","ans_value":1,
"ans_weightage":0,"is_default":0,"questions":{"id":1,
"question":"१. व्यक्तिको पुरा नाम थर ?","type":"input_edittext",
"description":"","param":"smalltext","maxlength":20,"dependency":"",
"answers":[{"id":0,"ans_label":"मुली",
"ans_value":1,"ans_weightage":0,"is_default":0,"questions":{"id":1,
"question":"१. व्यक्तिको पुरा नाम थर ?","type":"input_edittext",
"description":"","param":"smalltext","maxlength":20,"dependency":"",
我的数据库只插入了一行。在我的控制器上只找到一个问题列表。但每当抛出json输出时,它会重复很多相同的行,就像上面的json示例一样。
可能是什么问题?如果你找不到完整的解决方案,请你建议我以json格式重复相同值的原因吗?
答案 0 :(得分:0)
杰克逊在这里进入了一个循环。您的Questions
班级有Answers
的链接,该班级会返回Questions
。
<强>解决方案强>
将questions
字段中的Answers
字段或获取者标记为@JsonIgnored
。
答案 1 :(得分:0)
您可以尝试@JsonManagedReference
和@JsonBackReference
注释
为了让Jackson工作得很好,关系的两个方面之一不应该被序列化,以避免导致你的stackoverflow错误的infite循环。
@OneToMany(mappedBy = "questions",targetEntity = Answers.class, cascade = CascadeType.ALL)
@JsonManagedReference
private Set<Answers> answers = new HashSet<>();
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "ques_id", nullable = false)
@JsonBackReference
private Questions questions;
或
如果对获取某些实体数据不感兴趣,只需在任何一个类
中使用@JsonIgnore