无法编写JSON:没有为类org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer找到的序列化器

时间:2022-04-29 16:37:25

I am getting the below error in the POSTMAN client when I try to fetch one user details.

当我试图获取一个用户详细信息时,我在POSTMAN客户机中收到了下面的错误。

<html><head><title>Apache Tomcat/7.0.47 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>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 SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.entity.Person_$$_jvstfa5_0[&quot;handler&quot;]); 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 SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.entity.Person_$$_jvstfa5_0[&quot;handler&quot;])</h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u>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.SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.entity.Person_$$_jvstfa5_0[&quot;handler&quot;]); 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 SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.entity.Person_$$_jvstfa5_0[&quot;handler&quot;])</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>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.SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.entity.Person_$$_jvstfa5_0[&quot;handler&quot;]); 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 SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.entity.Person_$$_jvstfa5_0[&quot;handler&quot;])
    org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.java:213)
    org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:207)
    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:193)
    org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
</pre></p><p><b>root cause</b> <pre>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 SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.entity.Person_$$_jvstfa5_0[&quot;handler&quot;])
    com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:51)
    com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:25)
    com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:464)
    com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:504)
    com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:117)
    com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:108)
    com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1596)
    org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.java:210)
    org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:207)
    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:193)
    org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
</pre></p><p><b>note</b> <u>The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.47</h3></body></html>

Implementation class method for calling one user with single Id-

实现类方法,用于调用单个Id的用户。

@Override
    public Person getPerson(int personId) {
        assignSessionLocalVariable();
        Person person= (Person) session.load(Person.class, personId);
        person.setLast_accessed(new Date());
        try{
                Transaction tx=session.beginTransaction();
                session.update(person);
                tx.commit();
        }catch(Exception e){
            logger.error("#3 Error while updating time in getPerson ####["+e+"]");
        }
        person=null;
        person=(Person) session.get(Person.class, personId);
        logger.info("## Returning the records ##["+person+"]");
        session.close();
        return person;
    }

Controller Class-

控制器类- - -

@RequestMapping(value="/person/all",method = RequestMethod.GET,headers="Accept=application/json")
     public List<Person> getAllPerson(){
         List<Person> persons=personDAO.getAllPerson();
         logger.info("## Returning the persons !!!"+persons);
         logger.info("## Size of list is :["+persons.size()+"]");
         return persons;
     }

I am getting logs correctly and database operations correct, check below but when it comes to displaying data in Postman client, error is coming up. What should I do to get a json response correct in PostMan client.

我正在正确地获取日志和数据库操作,请参阅下面的内容,但是当涉及到在Postman客户机中显示数据时,会出现错误。如何在PostMan客户端中获得正确的json响应?

Logs -

日志——

  17:26:24,532  INFO PersonDAOImpl:64 - ## Returning the records ##[Person [id=2, name=Ravikant, last_accessed=Thu Dec 17 17:26:23 IST 2015]]
  17:26:24,558  INFO PersonController:38 - ## Returning the person with personId...2

1 个解决方案

#1


0  

I was checking online and got a solution for this, I downloaded jar file and imported into my project from the below link and created a class HibernateAwareObjectMapper in my project and added configuration for annotation in xml. Then, its done I am not getting any more such exceptions now.

我在网上查看并得到了解决方案,我下载了jar文件并从下面的链接导入到我的项目中,并在我的项目中创建了一个类HibernateAwareObjectMapper,并添加了xml注释的配置。那么,就这样吧,我现在再也没有这样的例外了。

Here's a link - https://github.com/FasterXML/jackson-datatype-hibernate

这里有一个链接——https://github.com/FasterXML/jackson-datatype-hibernate

#1


0  

I was checking online and got a solution for this, I downloaded jar file and imported into my project from the below link and created a class HibernateAwareObjectMapper in my project and added configuration for annotation in xml. Then, its done I am not getting any more such exceptions now.

我在网上查看并得到了解决方案,我下载了jar文件并从下面的链接导入到我的项目中,并在我的项目中创建了一个类HibernateAwareObjectMapper,并添加了xml注释的配置。那么,就这样吧,我现在再也没有这样的例外了。

Here's a link - https://github.com/FasterXML/jackson-datatype-hibernate

这里有一个链接——https://github.com/FasterXML/jackson-datatype-hibernate