重点技术-20181008-GSON 报错HibernateProxy. Forgot to register a type adapter?
摘要: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?
使用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;
}
@Override
public HibernateProxy read(JsonReader in) throws IOException {
throw new UnsupportedOperationException("Not supported");
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void write(JsonWriter out, HibernateProxy value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
// Retrieve the original (not proxy) class
Class<?> baseType = Hibernate.getClass(value);
// Get the TypeAdapter of the original class, to delegate the serialization
TypeAdapter delegate = context.getAdapter(TypeToken.get(baseType));
// Get a filled instance of the original class
Object unproxiedValue = ((HibernateProxy) value).getHibernateLazyInitializer()
.getImplementation();
// Serialize the value
delegate.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.
--------------------- 本文来自 xspwz123 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/xspwz123/article/details/71425545?utm_source=copy
重点技术-20181008-GSON 报错HibernateProxy. Forgot to register a type adapter?相关推荐
- 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 ...
- VMBox加载拷贝的vid报错E_INVALIDARG (0x80070057) Cannot register the hard disk 'D:\VMBox\win7.vdi' {7bca5a3
VMBox加载拷贝的vid报错E_INVALIDARG (0x80070057) Cannot register the hard disk 'D:\VMBox\win7.vdi' {7bca5a3e ...
- 关于配置双数据源报错org.apache.ibatis.binding.BindingException: Type interface is not known to the MapperRegis
报错org.apache.ibatis.binding.BindingException: Type interface is not known to the MapperRegistry.,在网上 ...
- auto cad安装报错command line option syntax error.type command
网上下载的cad2012安装包,自己解压出来按照发现报错command line option syntax error.type command,大概意思就是安装包不能包含中文目录,最后的解决办法就 ...
- SpringCloud - 整合Nacos启动报错Consider defining a bean of type IClientConfig
SpringCloud - 整合Nacos启动报错Consider defining a bean of type IClientConfig 前言 一. 尝试解决Bug的几种不合适方案 1.1 添加 ...
- 解决启动报错Consider defining a bean of type ‘xxx‘ in your configuration.
解决启动报错Consider defining a bean of type 'xxx' in your configuration. 报错截图 解决方法:查看注解是否同时存在@AllArgsCons ...
- 报错: Cannot convert value of type ‘java.lang.String‘ to required type
报错: Cannot convert value of type 'java.lang.String' to required type Caused by: java.lang.IllegalSta ...
最新文章
- BigInteger类实例的构造过程——JDK源码解析
- python实现高校教务管理系统_python实现教务管理系统
- 关于第三周——回顾与修正
- hapi和typescript构建项目(正在更新中)
- 书店售书最低价格问题
- 需求与业务的区别、需求设计与业务设计的区别
- 在日本做开发的日子(工作篇 序)
- 认识进程 java 1615387415
- python执行过程_Python threading模块condition原理及运行流程详解
- [leetcode-117]填充每个节点的下一个右侧节点指针 II
- Eclipse 快捷键整理
- java mongo分组统计_mongodb 分组 topN
- One Pixel Attack for Fooling Deep Neural Networks论文解读
- win7安装sqlserver2014
- android中java中的开方
- 合取范式 (CNF)
- 有一个XxY的网格,一个机器人只能走格点且只能向右或向下走,要从左上角走到右下角。请设计一个算法,计算机器人有多少种走法。 给定两个正整数int x,int y,请返回机器人的走法数目。保证x+y小
- 自步学习(Self-paced Learning)
- 电梯卫士等对计算机的应用,小班安全活动教案:电梯小卫士教案
- WAIC | 九章云极方磊:Hypernets——自动化机器学习的基础框架