在使用Mybatis的过程中,使用了resultMap延迟加载。

延迟加载:association联表查询的过程中,查询另外两个表的对象。而延迟加载是指只有在使用这两个对象的时候才会进行查询。

问题的产生,在我这里,我是直接通过路由url直接查询 StudentDto_2 对象。延迟加载的对象为 TClass 和 Assistant.

public class StudentDto_2 {
private Integer stuId;
private String stuNum; //学生编号,unique
private String studentName;
private Long birthDate;
private TClass tClass;
private Assistant assistant;
}

在序列化的过程中,会抛出以下异常。

2018-08-27 11:14:29.339 ERROR 34755 --- [nio-8089-exec-1] o.a.c.c.C.[.[.[/].[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.zimo.mybaties.util.Result["data"]->com.zimo.mybaties.dto.StudentDto_2_$$_jvstfe4_0["handler"])] with root cause

问题:Springmvc+mybatis,mybatis配置延迟加载时,json序列化异常

我目前的解决办法是关闭延迟加载,关闭后一切正常。

寻找另外一种方法

参考链接:https://blog.csdn.net/justinytsoft/article/details/53575236

在SpringBoot中,默认使用Jackson来实现java对象到json格式的序列化与反序列化。Jackson的转化是通过ObjectMapper来实现的。SpringBoot内置了一个ObjectMapper。我们通过下面的配置来

实现序列化Mybatis的延迟加载。

package com.zimo.mybaties.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Configuration; @Configuration
public class CustomerMapper extends ObjectMapper {
public CustomerMapper() {
this.setSerializationInclusion(JsonInclude.Include.NON_NULL);  //返回为null的值则去除,
this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);  //解决延迟加载的对象
}
}

返回参数中了多了一个代理对象handle

{
"code": 200,
"message": "获取学生信息",
-"data": {
"stuId": 22,
"stuNum": "12310",
"studentName": "天涯10",
"birthDate": 777524356000,
-"tClass": {
"classId": 3,
"classNum": "1921083145",
"className": "数学"
},
-"assistant": {
"assistantId": 1,
"assistantNum": "1315151521",
"name": "还在于是"
},
"handler": { }
}
}

转自:https://www.cnblogs.com/wbq1113/p/9541114.html

【转】使用Mybatis时遇到的延迟加载造成返回异常的问题——HttpMessageConversionException: Type definition error的更多相关文章

  1. mybatis入门基础(七)----延迟加载

    一.什么是延迟加载 resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能. 需求: ...

  2. mybatis高级(3)_延迟加载_深度延迟_一级缓存_二级缓存

    设置延迟加载需要在mybatis.xml中设置 注: 侵入式延迟加载为真时是延迟加载 侵入式延迟加载为假时是深度延迟加载 <!-- 延迟加载和深度延迟加载 --> <settings ...

  3. mybatis学习记录七——延迟加载

    14       延迟加载 14.1     什么是延迟加载 resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.co ...

  4. SSM框架之Mybatis(7)延迟加载、缓存及注解

    Mybatis(7)延迟加载.缓存及注解 1.延迟加载 延迟加载: 就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据.延迟加载也称懒加载. **好处:**先从单表查询,需要时再从关联表去关 ...

  5. 搭建mybatis时的小问题

    1.源文件中的xml文件经过编译后没有打包到classes中去,在源文件包中写的mapper文件运行时找不到. 解决办法: pom文件build下添加编译时加入xml和resource文件下的所有文件 ...

  6. 简单探讨spring整合mybatis时sqlSession不需要释放关闭的问题

    https://blog.csdn.net/RicardoDing/article/details/79899686 近期,在使用spring和mybatis框架编写代码时,sqlSession不需要 ...

  7. 学习mybatis时出现了java.io.IOException: Could not find resource EmployeeMapper.xml

    使用mybatis时出现了Could not find resource EmployeeMapper.xml和Could not find resource mybatis-config.xml两种 ...

  8. 使用IDEA工具整合mybatis时使用@Resource和@Autowired自动注解bean时会显示红色问题的解决办法

    使用IDEA工具整合mybatis时使用@Resource和@Autowired自动注解bean时会显示红色问题的解决办法 idea中springboot整合mybatis时,通过@Autowired ...

  9. Spring Boot 整合mybatis时遇到的mapper接口不能注入的问题

    现实情况是这样的,因为在练习spring boot整合mybatis,所以自己新建了个项目做测试,可是在idea里面mapper接口注入报错,后来百度查询了下,把idea的注入等级设置为了warnin ...

随机推荐

  1. mac 遇到的奇怪问题?

    1: 卸载 xcode,发现git报错了. mac git xcrun error active developer path 错误 解决办法:sudo xcode-select -switch / ...

  2. Redis主主复制、主从复制

    Redis主从复制(master-slaves配置)可以更好地读写分离 先关防火墙 主(master)服务器先不做任何操作, 进入从(slave)服务器, redis-3.2.1目录下的redis.c ...

  3. SQLServer 对已有数据表添加自增主键

    最近在做老表的数据整理,发现有的表没有主键标识,.NET Core 无法一键生成模型,需要带有主键的表才可以,所以需要针对已有数据添加主键,这是我找到的两种方式. 1. 主键为int 或者bigint ...

  4. mysql数据库连接出问题,提示超时 java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.解决办法

    mysql数据库连接出问题,提示超时,后来发现问题在于连接mysql数据库的jar包跟数据库版本不对应导致的,更换jar包一致就解决了.

  5. tensorflow中batch normalization的用法

    网上找了下tensorflow中使用batch normalization的博客,发现写的都不是很好,在此总结下: 1.原理 公式如下: y=γ(x-μ)/σ+β 其中x是输入,y是输出,μ是均值,σ ...

  6. 近期Freecodecamp问题总结

    最近没什么事,刷了freecodecamp的算法题,发现了自己基础的薄弱 1 where are thou 写一个 function,它遍历一个对象数组(第一个参数)并返回一个包含相匹配的属性-值对( ...

  7. linux下postgres未能正常启动的解决过程

    转载:http://www.cnblogs.com/starRebel/p/7892214.html 起因是一次linux服务器重启后,postgres没有起来,手动找原因. 1. 直接在命令行打po ...

  8. linux 因内存不足而 kill 掉 java 程序

    grep "Out of memory" /var/log/messages Sep 17 16:13:34 xxxaaa kernel: Out of memory: Kill ...

  9. scala基础学习(一)

    scala学习 scala与java的不同之处: 1.scala中任何一个操作符都是一个方法. s = 1+2;    可以写作:s=(1).+(2) 2.异常捕获采用模式匹配的方式. try { v ...

  10. bootStrap table 和 JS 开发过程中遇到问题汇总

    1..bootStrap-table表头固定 在table定义的时候给高度属性就可以自动生成滚动条,并且固定表头[height: 220,] 2.为动态生成的DOM元素绑定事件 on("cl ...