SpringMVC集成Hibernate返回json出现异常
2015-12-13 23:06
387 查看
查询返回json时出现的错误,json解析使用jackson
1. 问题描述
实体如下:
Classify(分类):
@Entity
public
classClassify {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer
classifyId;
private String
classifyName;
private Integer
classifyParentId;
public Integer getClassifyId(){
return
classifyId;
}
public
void setClassifyId(IntegerclassifyId) {
this.classifyId = classifyId;
}
public StringgetClassifyName() {
return
classifyName;
}
public
void setClassifyName(StringclassifyName) {
this.classifyName = classifyName;
}
public IntegergetClassifyParentId() {
return
classifyParentId;
}
public
voidsetClassifyParentId(Integer classifyParentId) {
this.classifyParentId =classifyParentId;
}
}
Note(笔记):
@Entity
public
classNote {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer
noteId;
private String
noteTime;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="classifyId")
private Classify
classify;
private String
keywords;
private String
noteName;
private String
noteContent;
private String
noteSummary;
}
Note与Classify的关系是:多对一的关系
然后再Controller中获取指定noteId的Note
@RequestMapping(value =
"/query/{id}", method = RequestMethod.GET)
public Note queryNoteById(@PathVariable("id") Integer id){
log.info(id+"");
Notenote = null;
try {
note= noteService.queryNote(id);
note.getClassify().getClassifyId();
}catch(Exception e) {
log.error("查找id为" + id
+ "的笔记出现异常", e);
}
return note;
}
因为1那端是使用的时候才加载,这时候,访问…/query/2,会出现异常
com.fasterxml.jackson.databind.JsonMappingException:No serializer found for classorg.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no propertiesdiscovered to create BeanSerializer (to avoid exception, disableSerializationFeature.FAIL_ON_EMPTY_BEANS)
) (through reference chain:com.pan.entity.dbentity.Note["classify"]->com.pan.entity.dbentity.Classify_$$_javassist_1["handler"])
com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:59)
如果1那端不是懒加载,那么则不会出现异常
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="classifyId")
private Classify
classify;
解决方案:
在关联实体类上标注@JsonIgnoreProperties注释
这里在Classify类上定义@JsonIgnoreProperties注释
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler"})
publicclassClassify {
。。。
}
上面两个值是必须的
1. 问题描述
实体如下:
Classify(分类):
@Entity
public
classClassify {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer
classifyId;
private String
classifyName;
private Integer
classifyParentId;
public Integer getClassifyId(){
return
classifyId;
}
public
void setClassifyId(IntegerclassifyId) {
this.classifyId = classifyId;
}
public StringgetClassifyName() {
return
classifyName;
}
public
void setClassifyName(StringclassifyName) {
this.classifyName = classifyName;
}
public IntegergetClassifyParentId() {
return
classifyParentId;
}
public
voidsetClassifyParentId(Integer classifyParentId) {
this.classifyParentId =classifyParentId;
}
}
Note(笔记):
@Entity
public
classNote {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer
noteId;
private String
noteTime;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="classifyId")
private Classify
classify;
private String
keywords;
private String
noteName;
private String
noteContent;
private String
noteSummary;
}
Note与Classify的关系是:多对一的关系
然后再Controller中获取指定noteId的Note
@RequestMapping(value =
"/query/{id}", method = RequestMethod.GET)
public Note queryNoteById(@PathVariable("id") Integer id){
log.info(id+"");
Notenote = null;
try {
note= noteService.queryNote(id);
note.getClassify().getClassifyId();
}catch(Exception e) {
log.error("查找id为" + id
+ "的笔记出现异常", e);
}
return note;
}
因为1那端是使用的时候才加载,这时候,访问…/query/2,会出现异常
com.fasterxml.jackson.databind.JsonMappingException:No serializer found for classorg.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no propertiesdiscovered to create BeanSerializer (to avoid exception, disableSerializationFeature.FAIL_ON_EMPTY_BEANS)
) (through reference chain:com.pan.entity.dbentity.Note["classify"]->com.pan.entity.dbentity.Classify_$$_javassist_1["handler"])
com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:59)
如果1那端不是懒加载,那么则不会出现异常
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="classifyId")
private Classify
classify;
解决方案:
在关联实体类上标注@JsonIgnoreProperties注释
这里在Classify类上定义@JsonIgnoreProperties注释
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler"})
publicclassClassify {
。。。
}
上面两个值是必须的
相关文章推荐
- XML 与 JSON 优劣对比
- VBA将excel数据表生成JSON文件
- newtonsoft.json解析天气数据出错解决方法
- vbs 解析json jsonp的方法
- Extjs4如何处理后台json数据中日期和时间
- C#实现将类的内容写成JSON格式字符串的方法
- JQuery ajax返回JSON时的处理方式 (三种方式)
- jquery JSON的解析方式示例介绍
- c#版json数据解析示例分享
- ASP JSON类文件的使用方法
- C#实现对Json字符串处理实例
- C#实现Json转Unicode的方法
- php解析json数据实例
- PHP中JSON的应用技巧
- PHP6 先修班 JSON实例代码
- 关于JSON以及JSON在PHP中的应用技巧
- .Net中的json操作类用法分析
- C#操作 JSON方法汇总
- JavaScript中字符串(string)转json的2种方法
- js Object2String方便查看js对象内容