一段时间以来,我一直在寻找此问题的答案/解决方法。
问题是,当将 Json 字符串转换为 Java 对象时,会抛出“私有(private) java.lang.Throwable java.lang.Throwable.cause 上不允许反射”的异常。
我知道 GAE 是一个沙盒环境,因此我无法按照自己的意愿突破界限,但是,我希望不必编写自己的反序列化器。
原因是我正在使用的数据类型非常复杂,因此编写自己的解串器不仅耗时,而且会破坏使用 Jackson 和 GSON 等框架的意义。
下面是 Jackson 和 GSON 的代码示例,两种实现都在本地工作(不是沙箱),但在部署到 GAE 时则不然。 jackson :
public class JsonConverterJackson {
private static final ObjectMapper mapper = new ObjectMapper().setVisibility(PropertyAccessor.FIELD, Visibility.NON_PRIVATE);
/** This method deserializes the specified Json into an object of the specified class.
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*
*/
public static <T> T convertFromJson(String toConvert, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException{
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper.readValue(toConvert, clazz);
}
/**
* This method serializes the specified object into its equivalent Json representation.
* @throws JsonProcessingException
*/
public static String convertToJson(Object toConvert) throws JsonProcessingException{
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper.writeValueAsString(toConvert);
}
GSON:
public class JsonConverter {
private static final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create();
//private static final Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.PRIVATE).setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create();
/** This method deserializes the specified Json into an object of the specified class.
*
*/
public static <T> T convertFromJson(String toConvert, Class<T> clazz){
return gson.fromJson(toConvert, clazz);
}
/**
* This method serializes the specified object into its equivalent Json representation.
*/
public static String convertToJson(Object toConvert){
return gson.toJson(toConvert);
}
jackson 的异常详细信息: 以下是 Jackson 的异常日志:
com.fasterxml.jackson.databind.JsonMappingException: Can not access private java.lang.Throwable java.lang.Throwable.cause (from class java.lang.Throwable; failed to set access: java.lang.IllegalAccessException: Reflection is not allowed on private java.lang.Throwable java.lang.Throwable.cause
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not access private java.lang.Throwable java.lang.Throwable.cause (from class java.lang.Throwable; failed to set access: java.lang.IllegalAccessException: Reflection is not allowed on private java.lang.Throwable java.lang.Throwable.cause
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:272)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:247)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:146)
at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:
任何建议都将受到欢迎。
最佳答案
从 POJO 中删除自定义异常字段就可以了。 使用 GAE 安全管理器,序列化为 Throwable 似乎是一个限制。 您无法访问除您自己的对象之外的任何对象的私有(private)字段。
关于java - Jackson 和 GSON 无法与 Google App Engine 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20710762/