org.springframework.http.converter.HttpMessageNotWritableException:

摘要:在使用spring+hibernate搭建RESTful API时,在Hibernate查询出数据实体类使用jackson序列化为json时,返回页面的时候报如下错误:


一:错误信息:

  1. HTTP Status 500 - 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 SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.HashMap[“user”]->com.qdexam.qdky.model.TblUser_
    _javassist_2["handler"]);&nbsp;nested&nbsp;exception&nbsp;is&nbsp;com.fasterxml.jackson.databind.JsonMappingException:&nbsp;No&nbsp;serializer&nbsp;found&nbsp;for&nbsp;class&nbsp;org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer&nbsp;and&nbsp;no&nbsp;properties&nbsp;discovered&nbsp;to&nbsp;create&nbsp;BeanSerializer&nbsp;(to&nbsp;avoid&nbsp;exception,&nbsp;disable&nbsp;SerializationFeature.FAIL_ON_EMPTY_BEANS)&nbsp;)&nbsp;(through&nbsp;reference&nbsp;chain:&nbsp;java.util.HashMap["user"]-</span><span class="tag">&gt;</span><span>com.qdexam.qdky.model.TblUser_
    _javassist_2[“handler”])  
HTTP Status 500 - 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 SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.HashMap["user"]->com.qdexam.qdky.model.TblUser_$$_javassist_2["handler"]); nested exception is 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.HashMap["user"]-&gt;com.qdexam.qdky.model.TblUser_$$_javassist_2["handler"])

  1. type Exception report  
  2.   
  3. message 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 SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.HashMap[“user”]->com.qdexam.qdky.model.TblUser_
    _javassist_2["handler"]);&nbsp;nested&nbsp;exception&nbsp;is&nbsp;com.fasterxml.jackson.databind.JsonMappingException:&nbsp;No&nbsp;serializer&nbsp;found&nbsp;for&nbsp;class&nbsp;org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer&nbsp;and&nbsp;no&nbsp;properties&nbsp;discovered&nbsp;to&nbsp;create&nbsp;BeanSerializer&nbsp;(to&nbsp;avoid&nbsp;exception,&nbsp;disable&nbsp;SerializationFeature.FAIL_ON_EMPTY_BEANS)&nbsp;)&nbsp;(through&nbsp;reference&nbsp;chain:&nbsp;java.util.HashMap["user"]-</span><span class="tag">&gt;</span><span>com.qdexam.qdky.model.TblUser_
    _javassist_2[“handler”])  
  4.   
  5. description The server encountered an internal error that prevented it from fulfilling this request.  
  6.   
  7. exception  
  8.   
  9. 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 SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.HashMap[“user”]->com.qdexam.qdky.model.TblUser_
    _javassist_2["handler"]);&nbsp;nested&nbsp;exception&nbsp;is&nbsp;com.fasterxml.jackson.databind.JsonMappingException:&nbsp;No&nbsp;serializer&nbsp;found&nbsp;for&nbsp;class&nbsp;org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer&nbsp;and&nbsp;no&nbsp;properties&nbsp;discovered&nbsp;to&nbsp;create&nbsp;BeanSerializer&nbsp;(to&nbsp;avoid&nbsp;exception,&nbsp;disable&nbsp;SerializationFeature.FAIL_ON_EMPTY_BEANS)&nbsp;)&nbsp;(through&nbsp;reference&nbsp;chain:&nbsp;java.util.HashMap["user"]-</span><span class="tag">&gt;</span><span>com.qdexam.qdky.model.TblUser_
    _javassist_2[“handler”])  
  10.     org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.java:196)  
  11.     org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)  
  12.     org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:148)  
  13.     org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:90)  
  14.     org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:189)  
  15.     org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:69)  
  16.     org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)  
  17.     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)  
  18.     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)  
  19.     org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)  
  20.     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)  
  21.     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)  
  22.     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)  
  23.     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)  
  24.     javax.servlet.http.HttpServlet.service(HttpServlet.java:617)  
  25.     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)  
  26.     javax.servlet.http.HttpServlet.service(HttpServlet.java:723)  
  27.     org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:152)  
  28.     org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  
  29.     org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)  
  30.     org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  
  31. root cause  
  32.   
  33. 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.HashMap[“user”]->com.qdexam.qdky.model.TblUser_$$_javassist_2[“handler”])  
  34.     com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:59)  
  35.     com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:26)  
  36.     com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:503)  
  37.     com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:639)  
  38.     com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)  
  39.     com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:491)  
  40.     com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:412)  
  41.     com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:27)  
  42.     com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:114)  
  43.     com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1887)  
  44.     org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.java:193)  
  45.     org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)  
  46.     org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:148)  
  47.     org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:90)  
  48.     org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:189)  
  49.     org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:69)  
  50.     org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)  
  51.     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)  
  52.     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)  
  53.     org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)  
  54.     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)  
  55.     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)  
  56.     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)  
  57.     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)  
  58.     javax.servlet.http.HttpServlet.service(HttpServlet.java:617)  
  59.     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)  
  60.     javax.servlet.http.HttpServlet.service(HttpServlet.java:723)  
  61.     org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:152)  
  62.     org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  
  63.     org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)  
  64.     org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  
  65. note The full stack trace of the root cause is available in the Apache Tomcat/6.0.44 logs.  
type Exception report

message 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 SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.HashMap["user"]->com.qdexam.qdky.model.TblUser_$$_javassist_2["handler"]); nested exception is 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.HashMap["user"]-&gt;com.qdexam.qdky.model.TblUser_$$_javassist_2["handler"])

description The server encountered an internal error that prevented it from fulfilling this request.

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 SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.HashMap["user"]->com.qdexam.qdky.model.TblUser_$$_javassist_2["handler"]); nested exception is 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.HashMap["user"]-&gt;com.qdexam.qdky.model.TblUser_$$_javassist_2["handler"])
    org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.java:196)
    org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)
    org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:148)
    org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:90)
    org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:189)
    org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:69)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:152)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
root cause

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.HashMap["user"]->com.qdexam.qdky.model.TblUser_$$_javassist_2["handler"])
    com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:59)
    com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:26)
    com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:503)
    com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:639)
    com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:491)
    com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:412)
    com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:27)
    com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:114)
    com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1887)
    org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.java:193)
    org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)
    org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:148)
    org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:90)
    org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:189)
    org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:69)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:152)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.44 logs.



二:解决办法:

关联类里面所有的集合都加入到@JsonIgnoreProperties
这个方法注解是放在类定义上面的,也就是说:
@JsonIgnoreProperties
class A
{

}

如果你定义的类是这样的:

  1. public class A{  
  2.     private String description;  
  3.     private Boolean enable;  
  4.     private Integer createUser;  
  5.     private Date createTime;  
  6.     private Integer updateUser;  
  7.     private Date updateTime;  
  8.     private Set operations = new HashSet(0);  
  9.     private Set roles = new HashSet(0);  
  10.     private Set menus = new HashSet(0);  
  11. }  
public 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必须添加!


三:这样你再重新访问查询出数据实体类使用jackson序列化为json时就不会再报错了,而是直接返回json格式的数据到浏览器端。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值