Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何防止RestController中的递归结果?_Spring Boot_Jpa - Fatal编程技术网

Spring boot 如何防止RestController中的递归结果?

Spring boot 如何防止RestController中的递归结果?,spring-boot,jpa,Spring Boot,Jpa,课程: @Entity @Table(name = "question") @Data @NoArgsConstructor public class Question { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column private String text; @OneToMany(mappedBy="question", fetch = FetchType.LAZY)

课程:

@Entity
@Table(name = "question")
@Data
@NoArgsConstructor
public class Question {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column
private String text;

@OneToMany(mappedBy="question", fetch = FetchType.LAZY)
private List<Answer> answers;

public void addAnswer(Answer answer){
    answer.setQuestion(this);
    this.answers.add(answer);
}
}

@Entity
@Table(name = "answer")
@Data
@NoArgsConstructor
public class Answer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column
private String text;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="question_id")
private Question question;
}
它不应该返回这样的结果吗:

{
"text": "something else again",
"question": {
    "id": 4,
    "text": "question 3",
    "answers": [
        {
            "id": 1,
            "text": "something"
         }
    ]
}

这取决于将对象序列化为JSON所使用的JSON库。如果您正在使用Jackson,您可以使用
@JsonIgnoreProperties
来打破循环:

@实体
@表(name=“问题”)
公开课问题{
[....]
@JsonIgnoreProperties(“问题”)
公共列表getAnswers(){
返回答案;
}
}
如果你得到答案,同样的问题可能会发生,因此你可能还需要打破这个循环:

@实体
@表(name=“answer”)
公开课答案{
[....]
@JsonIgnoreProperties(“答案”)
公共问题{
返回问题;
}
}

不要将实体用作控制器方法的返回值。了解3层architecture@Jens谢谢我在学习,所以一步一个脚印。谢谢肯。我安装了Jackson,它解决了这个问题。
{
"text": "something else again",
"question": {
    "id": 4,
    "text": "question 3",
    "answers": [
        {
            "id": 1,
            "text": "something",
            "question": {
                "id": 4,
                "text": "question 3",
                "answers": [
                    {
                        "id": 1,
                        "text": "something",
                        "question": {
                            "id": 4,
                            "text": "question 3",
                            "answers": [
                                {
                                    "id": 1,
                                    "text": "something",
                                    "question": {
                                        "id": 4,
                                        "text": "question 3",
                                        "answers": [
                                            {
                                                "id": 1,
                                                "text": "something",
                                                "question": {
                                                    "id": 4,
                                                    "text": "question 3",
                                                    "answers": [
                                                        {
                                                            "id": 1,
                                                            "text": "something",
                                                            "question": {
                                                                "id": 4,
                                                                "text": "question 3",
                                                                "answers": [
                                                                    {
                                                                        "id": 1,
                                                                        "text": "something",
                                                                        "question": {
                                                                            "id": 4,
                                                                            "text": "question 3",
                                                                            "answers": [
                                                                                {
                                                                                    "id": 1,
                                                                                    "text": "something",
                                                                                    "question": {
                                                                                        "id": 4,
                                                                                        "text": "question 3",
                                                                                        "answers": [
                                                                                            {
                                                                                                "id": 1,
                                                                                                "text": "something",
                                                                                                "question": {
                                                                                                    "id": 4,
                                                                                                    "text": "question 3",
                                                                                                    "answers": [
{
"text": "something else again",
"question": {
    "id": 4,
    "text": "question 3",
    "answers": [
        {
            "id": 1,
            "text": "something"
         }
    ]
}