转发请注明出处:http://www.cnblogs.com/shizhongtao/p/3680216.html
在前后台的交互中,经常需要把bean对象与xml或者json,这里就把自己以前遇到的问题做一下说明。
- 如果bean中没有配置one-to-many或者many-to-many等映射关系,转化时候一般没有问题
- 在配置了单项映射关系中,如果把(fetch = FetchType.LAZY)的话。当应用gson对其进行转换时候,会出现这个问题:
java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?
at com.google.gson.internal.bind.TypeAdapters$.write(TypeAdapters.java:)
at com.google.gson.internal.bind.TypeAdapters$.write(TypeAdapters.java:)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:)
at com.google.gson.internal.bind.ArrayTypeAdapter.write(ArrayTypeAdapter.java:)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$.write(ReflectiveTypeAdapterFactory.java:)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$.write(ReflectiveTypeAdapterFactory.java:)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$.write(ReflectiveTypeAdapterFactory.java:)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:)
at com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:)
at com.google.gson.Gson.toJson(Gson.java:)
at com.google.gson.Gson.toJson(Gson.java:)
at com.google.gson.Gson.toJson(Gson.java:)
at com.google.gson.Gson.toJson(Gson.java:)
at com.test.Botest.test2(Botest.java:)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:)
at java.lang.reflect.Method.invoke(Method.java:)
at org.junit.runners.model.FrameworkMethod$.runReflectiveCall(FrameworkMethod.java:)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:)
at org.junit.runners.ParentRunner$.run(ParentRunner.java:)
at org.junit.runners.ParentRunner$.schedule(ParentRunner.java:)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:)
at org.junit.runners.ParentRunner.access$(ParentRunner.java:)
at org.junit.runners.ParentRunner$.evaluate(ParentRunner.java:)
at org.junit.runners.ParentRunner.run(ParentRunner.java:)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:)原因应该是在gson进行转换json字符串时候,得不到映射实体(除了id以外)属性。如果想解决这个问题,最简单的方法就是把FetchType改成EAGER。或者你可以为gson注册一个转换器。如下:
Gson gson=new GsonBuilder().registerTypeAdapter(TXq.class, new JsonSerializer<TXq>() {
@Override
public JsonElement serialize(TXq src, Type typeOfSrc,
JsonSerializationContext context) {
JsonObject o=new JsonObject();
o.addProperty("pid", src.getPid());
//o.addProperty("name", src.getName());
return o;
}
}).create();上面的类的转换器中注释的那句会引发hibernate查询数据库。
- 如果你配置了映射的双向关联。那么使用json时候就会报错。因为他们互相引用,gson转换时候就会进入一个无穷的死循环中。这时候你可以利用注册转换器的方法(如上),或者把这个属性忽略掉。
小节:应该利用xmlstream等转化工具时候都会遇到这个问题。