Java:在Applet中使用Gson会导致SecurityException

我正在尝试在Java Applet中使用Google Gson,但是当我这样做时

Exception in thread “Thread-19” java.security.AccessControlException: access denied (java.lang.reflect.ReflectPermission suppressAccessChecks)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:74)
at com.google.gson.MappedObjectConstructor.getNoArgsConstructor(MappedObjectConstructor.java:85)
at com.google.gson.MappedObjectConstructor.constructWithNoArgConstructor(MappedObjectConstructor.java:62)
at com.google.gson.MappedObjectConstructor.construct(MappedObjectConstructor.java:53)
at com.google.gson.JsonObjectDeserializationVisitor.constructTarget(JsonObjectDeserializationVisitor.java:40)
at com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializationVisitor.java:56)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:109)
at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:51)
at com.google.gson.Gson.fromJson(Gson.java:495)
at com.google.gson.Gson.fromJson(Gson.java:444)
at com.google.gson.Gson.fromJson(Gson.java:396)
at com.google.gson.Gson.fromJson(Gson.java:372)
at org.jblux.client.network.GsonParser$1.run(GsonParser.java:32)
at java.security.AccessController.doPrivileged(Native Method)
at org.jblux.client.network.GsonParser.parseJson(GsonParser.java:36)
at org.jblux.client.network.PlayerDataFactory.getDataFromBase64(PlayerDataFactory.java:36)
at org.jblux.client.states.MainMenuState.update(MainMenuState.java:155)
at java.util.Observable.notifyObservers(Observable.java:142)
at org.jblux.client.network.ResponseWaiter.responseReceived(ResponseWaiter.java:33)
at org.jblux.client.network.ServerListener.notify_observers(ServerCommunicator.java:236)
at org.jblux.client.network.ServerListener.doCommand(ServerCommunicator.java:252)
at org.jblux.client.network.ServerListener.run(ServerCommunicator.java:218)

我在这里发现了另外两个相关的问题.
GSON on Google App Engine throws a Security Exception
Reflection Permission problems when using the GSON library in a applet

但是他们两个都没有我的答案.刚才说不使用Gson.
有什么方法可以向我的小程序授予“反射”权限?

更新:我正在切换应用程序以使用JNLP文件和Java Web Start,因为我一直无法弄清楚如何使Applet正常工作.万一有人发现,我将保留这个问题,但是如果您不想放弃Gson,则使用JNLP可能是唯一的选择.

解决方法:

我使用自定义解串器解决了这个问题.

我有一堂课,有两个成员,一个时间戳和一个双打列表.这是对我有用的代码.

GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(TimestampedValueList.class, new JsonDeserializer<TimestampedValueList>() {
        @Override
        public TimestampedValueList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            long timestampMs = json.getAsJsonObject().get("timestampMs").getAsLong();
            double[] valueList = context.deserialize(json.getAsJsonObject().get("valueList"), double[].class);
            return new TimestampedValueList(timestampMs, valueList);
        }
    });
gson = gsonBuilder.create();

希望这可以帮助任何人!

上一篇:从深层JSON获取值


下一篇:java-如何通过保留键的数据类型将json序列化为另一个json?