com.fasterxml.jackson.databind.JsonMappingException: No serializer found for

来源:互联网 发布:德尔康尼骨科知乎 编辑:程序博客网 时间:2024/05/22 06:18

先说下场景,有个实体类是和其他实体关联的,关联的实体配置了延迟加载。查询出来后用Jackson转成json返回到前端。实际上是在springmvc中配置MappingJackson2HttpMessageConverter转换器。

Hibernate 两个实体关联的时候,可能会采用延迟加载。 当使用延迟加载时,如果关联的对象没有使用。那么它不是为null的。hibernate 会有一个代理对象,填充在里面。当你使用这个关联对象时(代理对象),这个对象可以查出原来这个关联对象的值。使用jackson去转化实体类时,如果实体类里面有代理对象,就会报标题的错误。现在还不知道为什么。但是可以知道解决方案是让转化的实体类不包含代理对象,一种解决方案是关闭延迟加载,而是自己把关联的对象设置进去,覆盖代理对象。第三种就是使用@JsonIgnore注解,让jackson不去转化这个关联对象。
下面看下实例
这是项目实体类,有个分类实体类和它是一对多的关系,项目是多,分类是一。

@SuppressWarnings("serial")@Entity@Table(name = "project", schema = "microcat", catalog = "")public class ProjectEntity implements Serializable{    private long id;    private String name;    private Timestamp createTime;    private Timestamp editedTime;    private UserEntity editor;    private ClassificationEntity classification;    @ManyToOne(fetch=FetchType.LAZY)    @JoinColumn(name="EDITOR")    public UserEntity getEditor() {        return editor;    }    public void setEditor(UserEntity editor) {        this.editor = editor;    }    @ManyToOne(fetch=FetchType.LAZY) // 第一种@ManyToOne(fetch=FetchType.EAGE)    @JoinColumn(name="CLASSIFICATION")    @JsonIgnore // 第二种方案    public ClassificationEntity getClassification() {        return classification;    }    public void setClassification(ClassificationEntity classification) {        this.classification = classification;    }    @Id    @Column(name = "ID")    public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }    @Basic    @Column(name = "NAME")    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Basic    @Column(name = "CREATE_TIME")    public Timestamp getCreateTime() {        return createTime;    }    public void setCreateTime(Timestamp createTime) {        this.createTime = createTime;    }    @Basic    @Column(name = "EDITED_TIME")    public Timestamp getEditedTime() {        return editedTime;    }    public void setEditedTime(Timestamp editedTime) {        this.editedTime = editedTime;    }    @Override    public boolean equals(Object o) {        if (this == o) return true;        if (o == null || getClass() != o.getClass()) return false;        ProjectEntity that = (ProjectEntity) o;        if (id != that.id) return false;        if (name != null ? !name.equals(that.name) : that.name != null) return false;        if (createTime != null ? !createTime.equals(that.createTime) : that.createTime != null) return false;        if (editedTime != null ? !editedTime.equals(that.editedTime) : that.editedTime != null) return false;        return true;    }    @Override    public int hashCode() {        int result = (int) (id ^ (id >>> 32));        result = 31 * result + (name != null ? name.hashCode() : 0);        result = 31 * result + (createTime != null ? createTime.hashCode() : 0);        result = 31 * result + (editedTime != null ? editedTime.hashCode() : 0);        return result;    }}

未完待续

0 0
原创粉丝点击