ResponseBody 从对象中获取对象列表。 @JsonIgnore 不工作

ResponseBody get object list from object. @JsonIgnore not working

好的,我有 class 个用户

@Entity
@Table(name = "users")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private Long id_user;
    @Column(nullable = false)
    private String email;
    @Column(nullable = true)
    private String password;
    private String firstname;
    private String lastname;
    private Boolean enabled;
    private String phone;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_company")
    private Company company;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_rank")
    @JsonBackReference
    private Authority authorities;
    @OneToMany(mappedBy = "create_by")
    private List<Cms> create_by;
    @OneToMany(mappedBy = "modified_by")
    private List<Cms> modified_by;
    @OneToMany(mappedBy = "id_issuer")
    private List<Ticket> id_issuer;
    @OneToMany(mappedBy = "id_responsible")
    private List<Ticket> id_responsible;
    @OneToMany(mappedBy = "id_author")
    private List<IssueMsg> id_author;
    public User() {
    }

    public User(String email, String password, String firstname, String lastname, String phone, Company company, Authority authority) {
        this.email = email;
        this.password = password;
        this.firstname = firstname;
        this.lastname = lastname;
        this.enabled = true;
        this.phone = phone;
        this.authorities = authority;
    }

    @Override
    public String toString() {
        return "User [id_user=" + id_user + ", email=" + email + ", password="
                + password + ", firstname=" + firstname + ", lastname="
                + lastname + ", enabled=" + enabled + ", phone=" + phone
                + "]";
    }

    public Long getId_user() {
        return id_user;
    }

    public void setId_user(Long id_user) {
        this.id_user = id_user;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    @JsonIgnore
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    /**
     * @return the authorities
     */
    public Authority getAuthorities() {
        return authorities;
    }

    /**
     * @param authorities the authorities to set
     */
    public void setAuthorities(Authority authority) {
        this.authorities = authority;
    }
    @JsonIgnore
    public List<Cms> getCreate_by() {
        return create_by;
    }

    public void setCreate_by(List<Cms> create_by) {
        this.create_by = create_by;
    }
    @JsonIgnore
    public List<Cms> getModified_by() {
        return modified_by;
    }

    public void setModified_by(List<Cms> modified_by) {
        this.modified_by = modified_by;
    }
    @JsonIgnore
    public List<Ticket> getId_issuer() {
        return id_issuer;
    }

    public void setId_issuer(List<Ticket> id_issuer) {
        this.id_issuer = id_issuer;
    }
    @JsonIgnore
    public List<Ticket> getId_responsible() {
        return id_responsible;
    }

    public void setId_responsible(List<Ticket> id_responsible) {
        this.id_responsible = id_responsible;
    }
    @JsonIgnore
    public List<IssueMsg> getId_author() {
        return id_author;
    }

    public void setId_author(List<IssueMsg> id_author) {
        this.id_author = id_author;
    }
}

和class公司

@Entity
@Table(name = "companies")

public class Company implements Serializable {

    private static final long serialVersionUID = 6255059577246367312L;
    @Id
    @GeneratedValue
    private Long id_company;
    private String name;
    private String adress;
    private String email;
    private String phone;
    @OneToMany(mappedBy = "company")
    @JsonManagedReference
    private List<User> user;

    @Override
    public String toString() {
        return "Company [id_company=" + id_company + ", name=" + name
                + ", adress=" + adress + ", email=" + email + ", phone="
                + phone + "]";
    }

    public Company() {
    }

    public Company(Long id_company, String name, String adress, String email,
            String phone) {
        this.id_company = id_company;
        this.name = name;
        this.adress = adress;
        this.email = email;
        this.phone = phone;
    }

    public Long getId_company() {
        return id_company;
    }

    public void setId_company(Long id_company) {
        this.id_company = id_company;
    }

    public String getName() {
        return name;
    }

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

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
    public List<User> getUser() {
        return user;
    }

    public void setUser(List<User> user) {
        this.user = user;
    }

}

我从数据库中获取所有结果,并将其作为响应正文返回。

我的 JSON 响应得到 user.company.user 列表,我不需要什么。我尝试添加 getter 公司用户 @JsonIgnore,但出现错误

 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: java.util.ArrayList[1]->projektzespolowy.model.User["company"]->projektzespolowy.model.Company_$$_jvstcb8_2["handler"])

我阅读了很多 post 关于修复问题的文章,但没有人提供帮助。可以在公司忽略这个用户列表吗?

我认为您遇到此异常是因为 User#company 是延迟获取的,因此 jackson 尝试序列化休眠代理。尝试在映射上使用 fetch = FetchType.EAGER,或者在返回结果之前在控制器中调用 user.getCompany()