`
sxdtzhaoxinguo
  • 浏览: 228365 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

org.springframework.http.converter.HttpMessageNotWritableException:

 
阅读更多

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


一:错误信息:

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"]->com.qdexam.qdky.model.TblUser_$$_javassist_2["handler"])

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"]->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"]->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
{

}

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

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格式的数据到浏览器端。



分享到:
评论

相关推荐

    org.spring-framework-3.0.4. 所有jar

    org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework....

    org.springframework.flex-1.0.3.RELEASE.jar.zip

    org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包,org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3....

    org.springframework.core.jar

    《Spring框架核心模块——org.springframework.core.jar深度解析》 在Java世界中,Spring框架以其卓越的灵活性、可扩展性和模块化设计,成为了企业级应用开发的首选。其中,`org.springframework.core.jar`是Spring...

    spring-web-2.5.jar

    org.springframework.remoting.caucho.BurlapClientInterceptor.class org.springframework.remoting.caucho.BurlapProxyFactoryBean.class org.springframework.remoting.caucho.BurlapServiceExporter.class org....

    org.springframework.core_3.1.1.RELEASE.jar

    1. `org.springframework.core.convert`: 这个包包含了Spring的类型转换系统,如`Converter`和`ConverterFactory`接口,用于实现对象间的自动转换。`ConversionService`是转换服务的入口,它可以将任何对象转换为...

    spring3.0.5 所有jar文件

    org.springframework.aop-3.0.5.RELEASE.jar org.springframework.asm-3.0.5.RELEASE.jar org.springframework.aspects-3.0.5.RELEASE.jar org.springframework.beans-3.0.5.RELEASE.jar org.springframework....

    org.springframework.web的jar包.zip

    在Java开发领域,Spring框架以其强大的功能和灵活的设计闻名,而`org.springframework.web`包是Spring框架中的一个重要部分,它提供了处理HTTP请求和响应的核心工具,以及对WebSocket的支持。在本篇文章中,我们将...

    spring-mock.jar

    org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi.SimpleNamingContextBuilder.class org.springframework....

    org.springframework.web.jar

    9. **Integration with other Spring Modules**:`org.springframework.web.jar`与Spring框架的其他模块紧密集成,如Spring Core、Spring Beans、Spring AOP等,共同构建出一个完整的应用框架。 总的来说,`org....

    org.springframework.web.servlet-3.0.1.RELEASE-A.jar

    Error creating bean with name 'org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]: Initialization of bean failed;...

    org.springframework.transaction-3.1.2.RELEASE.zip

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.interceptor.TransactionInterceptor#0': Error setting property values; nested ...

    aopalliance-1.0.jar,org.springframework.aop-3.0.0.RELEASE.jar

    aopalliance-1.0.jar,org.springframework.aop-3.0.0.RELEASE.jar,org.springframework.jdbc-3.0.0.RELEASEorg.springframework.beans-3.0.0.RELEASE.jar等

    org.springframework.test-3.0.2.RELEASE.jar

    《Spring框架测试模块详解——基于org.springframework.test-3.0.2.RELEASE.jar》 Spring框架是Java领域中广泛使用的轻量级应用框架,它以其模块化、灵活的配置和强大的功能深受开发者喜爱。在Spring的众多模块中,...

    org.springframework.web.struts-3.1.0.M2.jar

    《Spring与Struts整合——基于org.springframework.web.struts-3.1.0.M2.jar的实践探索》 在Java Web开发领域,Spring框架以其强大的依赖注入和面向切面编程能力,以及丰富的模块支持,成为了广大开发者青睐的框架...

    spring3.1 官方全部jar包

    org.springframework.aop-3.1.RELEASE.jar org.springframework.asm-3.1.RELEASE.jar org.springframework.aspects-3.1.RELEASE.jar org.springframework.beans-3.1.RELEASE.jar org.springframework.context....

    通过import org.springframework.web.multipart.MultipartFile;上传文件

    通过import org.springframework.web.multipart.MultipartFile;上传文件的一个工具性的jar.

    org.springframework.context-sources-3.0.5.release.jar

    org.springframework.context-sources-3.0.5.release.jar

    java解决org.springframework.web.context.ContextLoaderListener

    在Java Web开发中,`org.springframework.web.context.ContextLoaderListener` 是Spring框架的一部分,它负责初始化一个Web应用程序的Spring上下文。这个监听器是基于Servlet容器(如Tomcat、Jetty等)的,当Web应用...

    org.springframework.asm-3.0.5.RELEASE.jar

    《深入解析Spring ASM库:org.springframework.asm-3.0.5.RELEASE.jar》 Spring框架是Java开发领域中不可或缺的一部分,而org.springframework.asm-3.0.5.RELEASE.jar是Spring框架中的一个关键组件,主要涉及到字节...

    org.springframework.web-2.5.6.A.jar

    《Spring Web框架详解——以org.springframework.web-2.5.6.A.jar为例》 在Java Web开发领域,Spring框架无疑是最重要的工具之一。今天我们将聚焦于一个特定的版本——`org.springframework.web-2.5.6.A.jar`,它是...

Global site tag (gtag.js) - Google Analytics