使用Gson序列化具有 transient 字段的对象

ecfsfe2w  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(254)

我正在尝试将一个对象转换为JSON,该对象具有InetSocketAddress示例作为属性。InetSocketAddress中的保持器属性被标记为 transient ,因此在序列化为JSON时被Gson忽略。
我不能删除transient关键字,因为InetSocketAddress类内置在java.net中。
我还尝试将excludeFieldsWithModifiers()用于transientstatic修饰符:

final Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create();
String json = gson.toJson(registration, Registration.class);

这会产生以下错误:

java.lang.reflect.InaccessibleObjectException: Unable to make field private static final long java.net.InetSocketAddress.serialVersionUID accessible: module java.base does not "opens java.net" to unnamed module @3e849b9e
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
    at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
    at com.google.gson.internal.reflect.UnsafeReflectionAccessor.makeAccessible(UnsafeReflectionAccessor.java:44)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:159)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:489)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:489)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:489)
    at com.google.gson.Gson.toJson(Gson.java:727)
    at com.google.gson.Gson.toJson(Gson.java:714)
    at com.google.gson.Gson.toJson(Gson.java:669)
    at org.eclipse.leshan.server.demo.websocket.WebSocketClient$1.registered(WebSocketClient.java:61)
    at org.eclipse.leshan.server.registration.RegistrationServiceImpl.fireRegistered(RegistrationServiceImpl.java:74)
    at org.eclipse.leshan.server.registration.RegistrationHandler$1.run(RegistrationHandler.java:86)
    at org.eclipse.leshan.core.response.SendableResponse.sent(SendableResponse.java:47)
    at org.eclipse.leshan.server.californium.registration.RegisterResource.handleRegister(RegisterResource.java:195)
    at org.eclipse.leshan.server.californium.registration.RegisterResource.handlePOST(RegisterResource.java:104)
    at org.eclipse.californium.core.CoapResource.handleRequest(CoapResource.java:219)
    at org.eclipse.leshan.core.californium.LwM2mCoapResource.handleRequest(LwM2mCoapResource.java:51)
    at org.eclipse.californium.core.server.ServerMessageDeliverer.deliverRequest(ServerMessageDeliverer.java:106)
    at org.eclipse.californium.core.network.stack.BaseCoapStack$StackTopAdapter.receiveRequest(BaseCoapStack.java:207)
    at org.eclipse.californium.core.network.stack.AbstractLayer.receiveRequest(AbstractLayer.java:84)
    at org.eclipse.californium.core.network.stack.AbstractLayer.receiveRequest(AbstractLayer.java:84)
    at org.eclipse.californium.core.network.stack.BlockwiseLayer.receiveRequest(BlockwiseLayer.java:488)
    at org.eclipse.californium.core.network.stack.ReliabilityLayer.receiveRequest(ReliabilityLayer.java:296)
    at org.eclipse.californium.core.network.stack.AbstractLayer.receiveRequest(AbstractLayer.java:84)
    at org.eclipse.californium.core.network.stack.BaseCoapStack.receiveRequest(BaseCoapStack.java:140)
    at org.eclipse.californium.core.network.CoapEndpoint$1.receiveRequest(CoapEndpoint.java:302)
    at org.eclipse.californium.core.network.UdpMatcher$2.run(UdpMatcher.java:289)
    at org.eclipse.californium.elements.util.SerialExecutor$1.run(SerialExecutor.java:290)
    at org.eclipse.californium.core.network.CoapEndpoint$6.run(CoapEndpoint.java:1311)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:833)

我该如何解决这个问题?

i2loujxw

i2loujxw1#

真实的的问题是gson库内部使用反射策略访问private字段,在java 9及以上版本中,不允许使用反射访问private。
一个可能的解决方案是,检查您使用的java版本。如果是Java 9或更高版本,则尝试降级到java 8。

ru9i0ody

ru9i0ody2#

这里的根本问题是,您(隐式地)使用基于反射的Gson类型适配器来处理第三方类(在本例中是JDK类)的私有字段。这样,您就依赖于该类的实现细节,这些细节可能在任何时候发生变化,或者如这里所示,可能变得不可访问。
这里“正确的”和面向未来的解决方案是为InetSocketAdress类编写一个自定义的TypeAdapter

相关问题