• SpringData JPA框架使用时出现JSON循环依赖解决方案


    困扰许久的问题终于解决了,之前项目太赶,没有深入学习解决,不甘心,今天再次搭起架子试试,哈哈,终于解决!

       
      @ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=false)
        @JoinColumn(name="approvalForCarId",nullable = false)
        private ApprovalForCar approvalForCar;
    
    
    
    
       @JsonIgnoreProperties(ignoreUnknown = true, value = {"approvalForCar"})
        @OneToMany(mappedBy = "approvalForCar",cascade = { CascadeType.MERGE,CascadeType.REMOVE},fetch = FetchType.EAGER)
        private List<DistributeCar> distributeCars = new ArrayList<>(0);
      双向关联时json序列化时会出现死循环的情况

    jackson解决问题的方式是循环的那一部分不解析
    JsonIgnoreProperties配置不解析的属性

    g:第一个“栗子",只有两个类关联,但互相引用了,多对多和一对一这里都适用
    Book类上面放入

    @JsonIgnoreProperties(ignoreUnknown = true, value =
    {"hibernateLazyInitializer", "handler", "fieldHandler"})
    public class Book{
    ... ...

    Book类中属性上注解,此属性Author中引用了private Set books;

    public class Book{
    ... ...
    @JsonIgnoreProperties(ignoreUnknown = true, value = {"books"})
    private Set<Author> authors;
    ... ...

    Author类上面放入

    @JsonIgnoreProperties(ignoreUnknown = true, value =
    {"hibernateLazyInitializer", "handler", "fieldHandler"})
    public class Author{
    ... ...

    Author类中属性上注解,此属性Book中引用了private Set authors;

    public class Author{
    ... ...
    @JsonIgnoreProperties(ignoreUnknown = true, value = {"authors"})
    private Set<Book> books;
    ... ...

    上面的例子“栗子"过去久远,只对关键需要配置的地方进行了标注
    eg:第二个“栗子",有三个类关联,通过一个中间类关联直接贴代码

    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

    import javax.persistence.*;
    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;

    @Entity
    @JsonIgnoreProperties(ignoreUnknown = true, value =
    {"hibernateLazyInitializer", "handler", "fieldHandler"})
    public class Book implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @TableGenerator(name = "book_generator", table = "tables_sequence", allocationSize = 1, initialValue = 0)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "book_generator")
    private int id;
    private String name;

    @OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
    @JsonIgnoreProperties(ignoreUnknown = true, value = {"book"})
    private Set<BookPublisher> bookPublishers;

    public Book() {
    }

    public Book(String name) {
    this.name = name;
    bookPublishers = new HashSet<>();
    }

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public Set<BookPublisher> getBookPublishers() {
    return bookPublishers;
    }

    public void setBookPublishers(Set<BookPublisher> bookPublishers) {
    this.bookPublishers = bookPublishers;
    }
    }

    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

    import javax.persistence.*;
    import java.io.Serializable;
    import java.util.Set;

    @Entity
    @JsonIgnoreProperties(ignoreUnknown = true, value =
    {"hibernateLazyInitializer", "handler", "fieldHandler"})
    public class Publisher implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @TableGenerator(name = "pub_generator", table = "tables_sequence", allocationSize = 1, initialValue = 0)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "pub_generator")
    private int id;
    private String name;
    @OneToMany(mappedBy = "publisher")
    @JsonIgnoreProperties(ignoreUnknown = true, value = {"publisher"})
    private Set<BookPublisher> bookPublishers;

    public Publisher() {

    }

    public Publisher(String name) {
    this.name = name;
    }

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public Set<BookPublisher> getBookPublishers() {
    return bookPublishers;
    }

    public void setBookPublishers(Set<BookPublisher> bookPublishers) {
    this.bookPublishers = bookPublishers;
    }
    }

    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

    import javax.persistence.*;
    import java.io.Serializable;
    import java.util.Date;

    @Entity
    @Table(name = "book_publisher")
    @JsonIgnoreProperties(ignoreUnknown = true, value =
    {"hibernateLazyInitializer", "handler", "fieldHandler"})
    public class BookPublisher implements Serializable {

    @Id
    @ManyToOne
    @JoinColumn(name = "book_id")
    @JsonIgnoreProperties(ignoreUnknown = true, value = {"bookPublishers"})
    private Book book;

    @Id
    @ManyToOne
    @JoinColumn(name = "publisher_id")
    @JsonIgnoreProperties(ignoreUnknown = true, value = {"bookPublishers"})
    private Publisher publisher;

    @Column(name = "published_date")
    private Date publishedDate;

    public Book getBook() {
    return book;
    }

    public void setBook(Book book) {
    this.book = book;
    }


    public Publisher getPublisher() {
    return publisher;
    }

    public void setPublisher(Publisher publisher) {
    this.publisher = publisher;
    }

    public Date getPublishedDate() {
    return publishedDate;
    }

    public void setPublishedDate(Date publishedDate) {
    this.publishedDate = publishedDate;
    }
    }


    总结:
    本实体中引入了另外一个实体,但另外一个实体也引用了自己无论是集合还是单个实体。jakson在格式化数据的时候会动态过滤掉此属性中对本身对象的引用。

  • 相关阅读:
    常见排序算法基于JS的实现
    PubSub的一种实现
    小结JS中的OOP(下)
    自己的一个LESS工具函数库
    小结JS中的OOP(中)
    小结JS中的OOP(上)
    表单的常用操作
    动手写插件之日历插件
    数组排序,字符串math,replace练习
    C++ Const(常类型)的作用总结
  • 原文地址:https://www.cnblogs.com/zzt-lovelinlin/p/10284229.html
Copyright © 2020-2023  润新知