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

时间:2015-01-16 16:37:39

标签: json spring spring-boot

好的我有班级用户

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

和班级公司

@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"])

我读了很多关于修复thix的帖子,但没有人帮忙。这可以忽略公司中的这个用户列表吗?

1 个答案:

答案 0 :(得分:1)

我认为你得到了这个异常,因为User#company是懒惰的,所以jackson试图序列化一个hibernate代理。在映射上尝试使用fetch = FetchType.EAGER,或在返回结果之前调用控制器中的user.getCompany()