在使用Spring+Hibernate搭建RESTful API时,在Hibernate查询出数据实体类使用jackson序列化为json时,报出org.springframework.web.servlet.DispatcherServlet – Handler execution resulted in exception – forwarding to resolved error view: ModelAndView: reference to view with name ‘uncaughtException’; model is {exception=org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )
解决办法:关联类里面所有的集合都加入到@JsonIgnoreProperties
这个方法注解是放在类定义上面的,也就是说:
@JsonIgnoreProperties
class A
{}
如果你定义的类是这样的:
class A{
private String description;
private Boolean enable;
private Integer createUser;
private Date createTime;
private Integer updateUser;
private Date updateTime;
private Set operations = new HashSet(0);
private Set roles = new HashSet(0);
private Set menus = new HashSet(0);
}
则把下面那句话加进去.
@JsonIgnoreProperties(value={“hibernateLazyInitializer”,”handler”,”operations”,”roles”,”menus”})
其中hibernateLazyInitializer,handler必须添加!