Gson序列化Spring bean

Ray*_*Ray 2 serialization spring json cglib gson

我在WebSphere 6.1上使用Gson 1.6和Spring Framework 3.0作为Java Web应用程序.我有一些Spring bean,实际的实例是CGLIB代理.当我尝试通过Gson序列化这些bean时,该类的非原始属性不是序列化的.取而代之的是:

{
   "CGLIB$BOUND":true,
   "CGLIB$CONSTRUCTED":true,
   "booleanProperty":true,
   "anotherBooleanProperty":true,
}
Run Code Online (Sandbox Code Playgroud)

在那里我期待更像的东西

{
   "stringProperty":"stringValue"
   "integerObjectProperty":17,
   "booleanProperty":true,
   "anotherBooleanProperty":true,
}
Run Code Online (Sandbox Code Playgroud)

当我序列化非代理POJO时,输出完全符合我的预期.如何让Gson生成我期望的输出?

Sea*_*oyd 5

我会说你的问题是一个不好的做法的结果.

Spring Beans通常由行为定义,而不是状态.并且您应该只序列化具有State而不是行为的类.

重构您的代码,将状态从Bean转移到Value Objects,并序列化它们.