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
- com.fasterxml.jackson.databind.JsonMappingException: No serializer found for
- com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class ...
- com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.apache.ibati
- com.fasterxml.jackson.databind.JsonMappingException:No suitable constructor found for type
- org.codehaus.jackson.map.JsonMappingException: No serializer found for class
- 【jackson 异常】com.fasterxml.jackson.databind.JsonMappingException异常处理
- zeppelin spark interpreter异常 com.fasterxml.jackson.databind.JsonMappingException
- 解决com.fasterxml.jackson.databind.JsonMappingException: No suitable 以 Jackson 处理复杂类型(List,map)两种方法
- org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy....
- JackSon将json串转成List<Object>,异常com.fasterxml.jackson.databind.JsonMappingException
- json异常com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type #657
- nested exception is com.fasterxml.jackson.databind.JsonMappingException: Conflicting getter definiti
- com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize out of START_ARRAY token
- jackson.databind之 com.fasterxml.jackson.databind.ser...错误
- com.fasterxml.jackson.databind.exc.MismatchedInputException
- com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
- com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
- Failed to read artifact descriptor for com.fasterxml.jackson.core:jackson-databind:jar:2.9.0-SNAPSHO
- py-faster-rcnn+cpu配置并训练自己的数据
- 【C语言】模拟实现strcpy()、strncpy()函数
- Note02
- python隐藏windows磁盘
- 第九章 CSS的继承、层叠、特殊性、重要性
- com.fasterxml.jackson.databind.JsonMappingException: No serializer found for
- 人脸检测+对齐之JDA
- Heartbleed心脏出血漏洞原理分析
- Android ART运行时无缝替换Dalvik虚拟机的过程分析
- 使用python实现MapReduce的wordcount实例
- python lmdb使用
- UE4 C++实现搜索指定目录下的指定类型的所有文件
- 简单用JDBC与mysql数据库进行连接
- sklearn——朴素贝叶斯文本分类