【问题标题】:Spring boot jpa manytomany json now fully shownSpring boot jpa manytomany json 现在完全显示
【发布时间】:2021-11-06 09:10:41
【问题描述】:
@Data
@Entity
@Table(name="course")

public class Course {

    @Id
    @GeneratedValue
    private Long id;
    private String title;
    private int credit;
    private String location;
    private String session;
    
    @ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL, mappedBy="courses")
    @JsonIgnore
    private List<Student> students = new ArrayList<>();
    
    public void addStudent(Student student) {
        students.add(student);
    }
    
    public void removeStudent(Student student) {
        students.remove(student);
    }
}
@Data
@Entity
@Table(name="student")

public class Student {

    @Id
    @GeneratedValue
    private Long id;
    private String firstName;
    private String lastName;
    private String contactNumber;
    private int GPA;
    private String email;
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "student_course",
        joinColumns = {@JoinColumn(name= "student_id")},
        inverseJoinColumns = {@JoinColumn(name="course_id")})
    
    private List<Course> courses = new ArrayList<>();
    
    public void addCourse(Course course) {
        courses.add(course);
    }
    
    public void removeCourse(Course course) {
        courses.remove(course);
    }
    
    
    
    
}

为了避免循环,我在 COURSE 一侧添加了@JsonIgnore,这使得 STUDENT 的输出看起来像这样:

 {
        "id": 7,
        "firstName": "Lil",
        "lastName": "Dogg",
        "contactNumber": "002002",
        "email": "lil@yahoo.com",
        "courses": [
            {
                "id": 2,
                "title": "M103",
                "credit": 3,
                "location": "Lab30",
                "session": "10:00"
            }
        ],
        "gpa": 2
    }

没关系。但在 COURSE 中,输出如下所示:

 {
        "id": 1,
        "title": "M105",
        "credit": 2,
        "location": "Lab50",
        "session": "08:00"
    },
    {
        "id": 2,
        "title": "M103",
        "credit": 3,
        "location": "Lab30",
        "session": "10:00"
    },

它省略了private List&lt;Student&gt; students = new ArrayList&lt;&gt;();

它没有显示在 Json 输出中!...当我在 Student 端添加 @JsonIgnore 时...它省略了 private List&lt;Course&gt; courses = new ArrayList&lt;&gt;();,如上所示!

如何解决?

【问题讨论】:

  • 明确地说,您希望显示这种双向关系,并且只是在研究如何处理它以使其不会无休止地递归?请参阅baeldung.com/… 和其他一些关于该主题的内容,因为有很多选项。
  • 与所有对象元素序列化的双向关系

标签: json spring spring-boot jpa many-to-many


【解决方案1】:

不要在控制器中将实体类呈现为 JSON 响应。相反,创建单独的DTOCourseDTOStudentDTO,您只需将所需的数据放入响应中并返回它们:

@GetMapping
public StudentDTO getStudent() {

【讨论】:

  • 除此之外,您还可以使用一些映射库,如模型映射器或 mapstruct,以避免将实体映射到 DTO 的样板代码
  • 我知道我应该使用 DTO 类,,,但是它解决了我的问题吗?
  • 是的,因为您不会在发回学生时填充 courses.students 列表,或者在发回课程时填充 students.courses 列表。在序列化发生之前,您可以在根据使用要求构建课程/学生图的 DTO 表示时决定什么是相关的,什么不相关。
猜你喜欢
  • 1970-01-01
  • 2020-02-04
  • 2017-02-02
  • 2019-06-16
  • 2019-06-16
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 2016-09-19
相关资源
最近更新 更多