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

jackson解决问题的方式是循环的那一部分不解析

  1. JsonIgnoreProperties配置不解析的属性
  2. 描述不形象,我用两个“栗子"来描述一下
  3. 类有多少个怎么关联都不用慌,你从你想要出发的类开始,属性中有对自己引用的就在属性那个类中将自己忽略

eg:第一个“栗子",只有两个类关联,但互相引用了,多对多和一对一这里都适用
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在格式化数据的时候会动态过滤掉此属性中对本身对象的引用。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值