1

我正在使用 Angular 5 和 Java Spring Boot 多模块应用程序 Employee 类和地址有一对多的关系。我也添加了@JsonManagedReference,@JsonBackReference注释,但在运行 Angular 代码时出现错误:

 Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.example.demo.model.Employee_$$_jvstdec_1["handler"])

实体类如下:

package com.example.demo.model;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonBackReference;

@Entity
@JsonIgnoreProperties(ignoreUnknown=true)
public class Address implements Serializable{

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int addId;    
  private int no; 
  private String addLine1;
  private String addLine2;
  private String addLine3;

  @ManyToOne
  @JoinColumn(name = "empId")
  @JsonBackReference
  private Employee employee;

  public int getAddId() {
    return addId;
  }
  public void setAddId(int addId) {
    this.addId = addId;
  }
  public int getNo() {
    return no;
  }
  public void setNo(int no) {
    this.no = no;
  }
  public String getAddLine1() {
    return addLine1;
  }
  public void setAddLine1(String addLine1) {
    this.addLine1 = addLine1;
  }
  public String getAddLine2() {
    return addLine2;
  }
  public void setAddLine2(String addLine2) {
    this.addLine2 = addLine2;
  }
  public String getAddLine3() {
    return addLine3;
  }
  public void setAddLine3(String addLine3) {
    this.addLine3 = addLine3;
  }
  public Employee getEmployee() {
    return employee;
  }
  public void setEmployee(Employee employee) {
    this.employee = employee;
  }
}


package com.example.demo.model;

  import java.io.Serializable;
  import java.util.List;


  import javax.persistence.CascadeType;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.OneToMany;
  import com.fasterxml.jackson.annotation.JsonManagedReference;
  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

    @Entity
    @JsonIgnoreProperties(ignoreUnknown=true)
    public class Employee implements Serializable {

      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private Long empId;
      private String empNo;
      private String empName;
      private String position;  
      @OneToMany(mappedBy="employee", cascade=CascadeType.ALL)
      @JsonManagedReference
      private List<Address> addresses;

      public Long getEmpId() {
        return empId;
      }

      public void setEmpId(Long empId) {
        this.empId = empId;
      }

      public String getEmpNo() {
        return empNo;
      }

      public void setEmpNo(String empNo) {
        this.empNo = empNo;
      }

      public String getEmpName() {
        return empName;
      }

      public void setEmpName(String empName) {
        this.empName = empName;
      }

      public String getPosition() {
        return position;
      }

      public void setPosition(String position) {
        this.position = position;
      }

      public List<Address> getAddresses() {
        return addresses;
      }

      public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
      }

    }
4

2 回答 2

1

如果有一个空 bean 并且 JSON 尝试序列化,则会发生这种情况。在您的情况下,您正在尝试序列化可能会或可能不会加载的延迟加载属性。
在您的ObjectMapper中,配置以下内容:

objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

应该可以正常工作。

于 2018-06-29T08:36:08.373 回答
0

您需要提供一个默认构造函数,以及一个必需的参数构造函数,以便 JPA(或 Hibernate 本身)可以在您需要的状态下正确初始化实体。

于 2018-06-29T08:39:10.230 回答