Java Jackson正在返回重复的行
我试图从这两个实体类返回json Questions.javaJava Jackson正在返回重复的行,java,spring,hibernate,rest,jackson,Java,Spring,Hibernate,Rest,Jackson,我试图从这两个实体类返回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 Strin
@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;
}
}
@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输出,它都会重复许多相同的行,如上面的json示例中所示
可能有什么问题?如果你找不到完整的解决方案,你能告诉我在json格式中复制相同值的原因吗 杰克逊在这里陷入了困境。您的
问题
类有一个指向答案
的链接,该类将返回到问题
解决方案
将
问题
字段或答案
类中的getter标记为@JsonIgnored
您可以尝试@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;
@OneToMany(mappedBy=“questions”,targetEntity=Answers.class,cascade=CascadeType.ALL)
@JsonManagedReference
私有集answers=newhashset();
@manytone(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name=“ques_id”,nullable=false)
@JsonBackReference
私人问题;
或
如果对获取一些实体数据不感兴趣,只需在类中的任何一个中使用
@JsonIgnore
,你怎么能期望有人读取你的非匹配json。。
@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;