zoukankan      html  css  js  c++  java
  • mybatis的级联查询-懒加载遇到的序列化问题

    不获取子查询的属性就不会触发子查询

    例如:

        @RequestMapping(value = "/searchTravelTemplateById1", method = RequestMethod.GET)
        public void searchTravelTemplateById1(Integer id, HttpServletResponse response) {
            TourElectronicTravelTemplate travelTemplate = travelTemplateService.queryTravelTemplateById(id);
        }

    如果有返回值,并且要序列化就会触发子查询,会使用同步查询所有结果,会有 N+1 问题

        @RequestMapping(value = "/searchTravelTemplateById1", method = RequestMethod.GET)
        public TourElectronicTravelTemplate searchTravelTemplateById1(Integer id, HttpServletResponse response) {
            TourElectronicTravelTemplate travelTemplate = travelTemplateService.queryTravelTemplateById(id);
            return travelTemplate;
        }

    可能会出现一个bug:

    2021-06-24 13:54:17.414 ----> [http-nio-8080-exec-1] ---> ERROR o.a.c.c.C.[.[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.tuniu.data.entity.vo.ResponseVo["data"]->com.tuniu.data.travle.entity.TourElectronicTravelTemplate_$$_jvste21_0["handler"])] with root cause
    com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.tuniu.data.entity.vo.ResponseVo["data"]->com.tuniu.data.travle.entity.TourElectronicTravelTemplate_$$_jvste21_0["handler"])

    可以在要返回的实体类属性上加入注解:

    @JsonIgnoreProperties(value = {"handler"})
    例如:
    @JsonIgnoreProperties(value = {"handler"})
    public class TourElectronicTravelTemplate {

    在此标记不生成json对象的属性

    因为jsonplugin用的是java的内审机制.hibernate会给被管理的pojo加入一个 handler 属性,jsonplugin会把 handler 也拿出来操作,并读取里面一个不能被反射操作的属性就产生了这个异常.  

     

     
     
     
     
  • 相关阅读:
    【mpeg2】MPEG-2官方参考代码MPEG2_reference_software
    【base】Copyright 与 Copyleft
    【base】Copyright 与 Copyleft
    【complier】如何查看ARM交叉编译的可执行程序依赖的动态库?
    【shell系列】之查看shell脚本的执行过程和makefile中调试手段
    【tools】一款强大的局部搜索工具:xsearch
    【tools】一款强大的局部搜索工具:xsearch
    【mpeg2】mpeg2编码器的开源实现:x262
    【mpeg2】mpeg2解码器开源实现:libmpeg2
    【codecs】视频显示分辨率格式分析
  • 原文地址:https://www.cnblogs.com/lzghyh/p/14926663.html
Copyright © 2011-2022 走看看