GSON 报错HibernateProxy. Forgot to register a type adapter?
来源:互联网 发布:渠道管理 知乎 编辑:程序博客网 时间:2024/06/05 05:52
使用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.
- GSON 报错HibernateProxy. Forgot to register a type adapter?
- GSON 报错HibernateProxy. Forgot to register a type adapter? 的解决办法
- Hibernate使用Gson时报错:org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?
- org.hibernate.proxy.HibernateProxy Forgot to register a type adapter?
- 异常org.hibernate.proxy.HibernateProxy.Forgot to register a type adapter
- org.hibernate.proxy.HibernateProxy.Forgot to register a type adapter 解决方案
- Attempted to serialize java.lang.Class Forgot to register a type adapte
- Register an InstanceCreator with Gson for this type to fix this problem.
- Button cannot be resolved to a type报错
- 关于WebServlet cannot be resolved to a type报错
- 报错:BASE64Encoder cannot be resolved to a type
- XX cannot be resolved to a type 报错
- Myeclipse工程项目报错connot be resolved to a type
- hibernate报错:JoinColumn cannot be resolved to a type
- 报错Test cannot be resolved to a type
- iOS报错--Init methods must return a type related to the receiver type
- Register a panel to kernel
- Register a Meta Type in Qt
- 安装Windows和Ubuntu双系统
- 了解容器
- java中long类型转换为int类型
- 苏嵌单片机(一)
- 卡尔曼滤波的原理说明
- GSON 报错HibernateProxy. Forgot to register a type adapter?
- dao--4.连接池
- Nonce、Timestamp——解决Replay-Attack问题
- Vue.Js2.0建议学习路线
- html标签一
- Gogs使用详解
- 最邻近规则分类算法(KNN)
- Java实现逆波兰表达式(Evaluate Reverse Polish Notation)
- Firefox定位网页元素工具