Java 无法写入HTTP消息:org.springframework.HTTP.converter.HttpMessageNotWritableException:无法写入JSON:错误

Java 无法写入HTTP消息:org.springframework.HTTP.converter.HttpMessageNotWritableException:无法写入JSON:错误,java,json,spring-boot,angular5,mappingexception,Java,Json,Spring Boot,Angular5,Mappingexception,我正在使用Angular 5和Java Spring Boot多模块应用程序Employee类和address具有一对多关系。我还添加了@JsonManagedReference,@JsonBackReference注释,但我是 运行角度代码时出错: Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No

我正在使用Angular 5和Java Spring Boot多模块应用程序Employee类和address具有一对多关系。我还添加了
@JsonManagedReference
@JsonBackReference
注释,但我是 运行角度代码时出错:

 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;
      }

    }
package com.example.demo.model;
导入java.io.Serializable;
导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
导入javax.persistence.JoinColumn;
导入javax.persistence.manytone;
导入com.fasterxml.jackson.annotation.JsonIgnoreProperties;
导入com.fasterxml.jackson.annotation.JsonBackReference;
@实体
@JsonIgnoreProperties(ignoreUnknown=true)
公共类地址实现可序列化{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私营企业;
私人互联网号码;
私有字符串addLine1;
私有字符串addLine2;
私有字符串addLine3;
@许多酮
@JoinColumn(name=“empId”)
@JsonBackReference
私人雇员;
公共int getAddId(){
返回ADID;
}
公共void setAddId(int addId){
this.addId=addId;
}
公共int getNo(){
返回否;
}
公共无效设置编号(内部编号){
这个。否=否;
}
公共字符串getAddLine1(){
返回addLine1;
}
公共void setAddLine1(字符串addLine1){
this.addLine1=addLine1;
}
公共字符串getAddLine2(){
返回addLine2;
}
公共void setAddLine2(字符串addLine2){
this.addLine2=addLine2;
}
公共字符串getAddLine3(){
返回addLine3;
}
公共void setAddLine3(字符串addLine3){
this.addLine3=addLine3;
}
公共雇员getEmployee(){
返回员工;
}
公共作废集合雇员(雇员雇员){
this.employee=employee;
}
}
包com.example.demo.model;
导入java.io.Serializable;
导入java.util.List;
导入javax.persistence.CascadeType;
导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
导入javax.persistence.OneToMany;
导入com.fasterxml.jackson.annotation.JsonManagedReference;
导入com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@实体
@JsonIgnoreProperties(ignoreUnknown=true)
公共类Employee实现了可序列化{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长empId;
私有字符串empNo;
私有字符串名称;
私有字符串位置;
@OneToMany(mappedBy=“employee”,cascade=CascadeType.ALL)
@JsonManagedReference
私人名单地址;
公共长getEmpId(){
返回empId;
}
公共无效setEmpId(长empId){
this.empId=empId;
}
公共字符串getEmpNo(){
返回empNo;
}
public void setEmpNo(字符串empNo){
this.empNo=empNo;
}
公共字符串getEmpName(){
返回empName;
}
public void setEmpName(字符串empName){
this.empName=empName;
}
公共字符串getPosition(){
返回位置;
}
公共无效设置位置(字符串位置){
这个位置=位置;
}
公共列表getAddresses(){
返回地址;
}
公共地址(列表地址){
this.address=地址;
}
}

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

objectMapper.configure(在\u空\u bean上的SerializationFeature.FAIL\u,false)


应该可以正常工作。

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