HibernateProxy. Forgot to register a type adapter?
2019独角兽企业重金招聘Python工程师标准>>>
使用Gson转换Hibernate对象遇到一个问题,当对象的Lazy加载的,就会出现上面的错误。处理方式摘抄自网上,留存一份以后自己看。
/*** This TypeAdapter unproxies Hibernate proxied objects, and serializes them* through the registered (or default) TypeAdapter of the base class.*/
public class HibernateProxyTypeAdapter extends TypeAdapter<HibernateProxy> {public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {@Override@SuppressWarnings("unchecked")public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {return (HibernateProxy.class.isAssignableFrom(type.getRawType()) ? (TypeAdapter<T>) new HibernateProxyTypeAdapter(gson) : null);}};private final Gson context;private HibernateProxyTypeAdapter(Gson context) {this.context = context;}@Overridepublic HibernateProxy read(JsonReader in) throws IOException {throw new UnsupportedOperationException("Not supported");}@SuppressWarnings({"rawtypes", "unchecked"})@Overridepublic void write(JsonWriter out, HibernateProxy value) throws IOException {if (value == null) {out.nullValue();return;}// Retrieve the original (not proxy) classClass<?> baseType = Hibernate.getClass(value);// Get the TypeAdapter of the original class, to delegate the serializationTypeAdapter delegate = context.getAdapter(TypeToken.get(baseType));// Get a filled instance of the original classObject unproxiedValue = ((HibernateProxy) value).getHibernateLazyInitializer().getImplementation();// Serialize the valuedelegate.write(out, unproxiedValue);}
}
实现上面的类,然后就是使用:
GsonBuilder b = new GsonBuilder();
...
b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
...
Gson gson = b.create();
解释,只是看不懂。
GSON contains a number of TypeAdapterFactory
implementations, for various types (primitive types, common types like String
or Date
, lists, arrays...). Each factory is asked if it is able to serialize a certain Java type (the parameter to create
is a TypeToken
instead of a Class
in order to capture possible information about generic types, which Class
does not have). If the factory is able to serialize/deserialize a type, it responds with a TypeAdapter
instance; otherwise it responds with null
.
HibernateProxyTypeAdapter.FACTORY
verifies whether type implements HibernateProxy
; in that case, it returns an instance of HibernateProxyTypeAdapter
for serialization. The write
method is called when an actual object has to be serialized; the adapter extracts the original type of the underlying object, and asks GSON for the standard TypeAdapter
for the original type, which generally is a ReflectiveTypeAdapter
.
Then it retrieves an instance of the original class, instead of directly using the proxy. This is necessary because ReflectiveTypeAdapter
accesses directly to fields, instead of using getters; accessing to the fields of a proxied object does not work, and is a classical Hibernate pitfall.
As a possible performance improvement, the delegate TypeAdapter
should be acquired in the create
method. I found out that calling getSuperclass()
on the proxy Class
appears to yield the original base class.
转载于:https://my.oschina.net/u/2398421/blog/664904
HibernateProxy. Forgot to register a type adapter?相关推荐
- 重点技术-20181008-GSON 报错HibernateProxy. Forgot to register a type adapter?
摘要: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a ...
- springboot出现“org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?“的解决办法
springboot出现"Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot ...
- 异常org.hibernate.proxy.HibernateProxy.Forgot to register a type adapter
今天遇到一个这样的问题,发现是是对象代理的原因,将转换json对象中的代理对象赋为空即可: <pre name="code" class="java"&g ...
- Attempted to serialize java.lang.Class: com.cao.bean.DmCategory. Forgot to register a type adapter?
使用mybatis从数据库获取的数据,用gson进行传参出现错误以下报错: 我使用的mybatis是采取懒加载的方式查询数据库,也就是只有用到了才去查真正的数据,用不到的话只是返回一个代理对象,gso ...
- Attempted to serialize java.lang.Class Forgot to register a type adapte
今天使用Mybatis练手,准备将db中查出来的结果集序列化,使用Gson 代码如下 List<User> users1 = userMapper.selectUserCollectio ...
- 《哈迪斯》经济系统分析
经济循环示意图 哈迪斯.png 经济系统 经济系统主要分为两部分,关内经济系统和关外经济系统.两部分经济资源可以转换,但是转换有条件.数量限制. 关内 关内主要资源为金币,金币无法在关卡外使用,金币大 ...
- 设计模式--适配器(Adapter)模式
模式定义 将一个类的接口转换成客户希望的另一个接口,适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作 类图 应用场景 1.当你希望使用某些现有类,但其接口与你的其他代码不兼容时: 2 ...
- 模式设计(七)Adapter
之前我们已经介绍完了创建型模式,它们分别为Factory Method,Abstract Factory,Singleton,Builder,Prototype.创建型模式是创建对象而不是直接实例化对 ...
- 简单读!tomcat源码(一)启动与监听
tomcat 作为知名的web容器,很棒! 本文简单了从其应用命令开始拆解,让我们对他有清晰的了解,揭开神秘的面纱!(冗长的代码流水线,给你一目了然) 话分两头: 1. tomcat是如何启动的? 2 ...
- ant design vue input change_vue 表单
文档:https://cn.vuejs.org/v2/guide/forms.html#ad 基本用法 使用vue脚手架 安装Nodejs 安装yarn:https://yarn.bootcss.co ...
最新文章
- weblogic创建域生产模式,输入用户名闪退
- 如何编写webService接口
- HTML5:一些部件
- python crawler(2)
- Codeforces 930 A. Peculiar apple-tree (dfs)
- css为网页顶部和底部都加入背景图
- vscode tab键快捷生成元素html标签
- 在win8下安装使用java及在win8下部署java环境变量-图文
- MATLAB点击运行并计时没反应,MATLAB计时器对象陷阱和不良用法
- 无刷新三级联动(ajax)(转)
- LeetCode4 寻找两个正序数组的中位数
- 开源监控软件 Hyperic 的两种插件
- AD7124采集工作流程
- 最值钱的程序员打法 机器学习从入门到精通50篇+自学python全套教程!
- sql注入攻击与防御java_注入攻击(SQL注入防御)
- 在有已认证的公众号情况下,复用资质快速注册小程序,免除再次认证费用
- Navicat Premium 历史版本下载地址(官方)
- 全国369个城市空气质量月度数据(2013-2021)
- 晨兴刘芹:避开同质化竞争 看好云适配技术创新
- 大型网站的监控、报警与故障转移
热门文章
- matlab如何把散点放大,如何在matlab中更新散点3图(循环)
- Anaconda3 + Python3.6 + PyTorch0.4.0 安装步骤
- linux运行nodejs一般路径_Linux上安装Nodejs
- excel字符串和单元格拼接_Excel工作表中带单位求和的4个技巧,不掌握就真的Out了!...
- Selenium中的xpath定位
- linux下tab作用的描述?
- 001_chrome工具详解
- Oracle EBS-SQL (PO-10):检查过期采购未接收订单.sql
- 物联网培训总结(C#)
- python struct 45s_python32版本,struct.error: argument for 's' must be a bytes object - 小众知识...