我已更新到jackson-databind 2.6.0以修复反序列化错误。但是,现在我收到一个新版本(我使用版本2.4.0和2.5.4检查了相同的测试,两者都有效)。
以下是例外:
java.lang.ArrayIndexOutOfBoundsException: 56
at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.withProperty(BeanPropertyMap.java:177)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder.build(BeanDeserializerBuilder.java:344)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:251)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:142)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:403)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:352)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:461)
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3804)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3698)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2714)
at com.mleitner.businessmaximizer.webservice.JacksonMapperTest.testOneTimeCustomer(JacksonMapperTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
测试很简单:
@Test
public void testOneTimeCustomer() throws Exception {
OneTimeCustomerDTO customer = new OneTimeCustomerDTO("Max", null, null, null, null, null, null, null);
ObjectMapper om = new MyObjectMapper();
String result = om.writeValueAsString(customer);
System.out.println("Result:" + result);
om.readValue(result, OneTimeCustomerDTO.class);
}
and has this output: Result:{"@id":1,"firstName":"Max"}
类MyObjectMapper做了一些我一直使用的配置。配置设置为:
this.setVisibility(
getSerializationConfig().
getDefaultVisibilityChecker().
withFieldVisibility(JsonAutoDetect.Visibility.ANY).
withGetterVisibility(JsonAutoDetect.Visibility.NONE).
withSetterVisibility(JsonAutoDetect.Visibility.NONE).
withCreatorVisibility(JsonAutoDetect.Visibility.NONE).
withIsGetterVisibility(JsonAutoDetect.Visibility.NONE));
this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
this.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
this.setSerializationInclusion(Include.NON_NULL);
我有一个AnnotationIntrospector,它为递归结构添加了id:
@Override
public ObjectIdInfo findObjectIdInfo(final Annotated ann) {
if (BaseDTO.class.isAssignableFrom(ann.getRawType())) {
// this is needed for recursive structures
return new ObjectIdInfo(PropertyName.construct("@id", null),
ann.getRawType(),
ObjectIdGenerators.IntSequenceGenerator.class, null);
}
return super.findObjectIdInfo(ann);
}
我的DTO看起来像这样: OneTimeCustomerDTO-> CustomerDTO(摘要) - > BaseDTO(摘要)
OneTimeCustomerDTO有8个String属性,CustomerDTO 1 String和1个long属性。两者都有复杂的属性(其他类),但在测试期间它们为空,所以我认为它们不相关。
如果我删除ObjectIdInfo它也适用于2.6.0 ...但我需要这个来传递递归结构。
所以我的问题是:
提前感谢您的时间和精力。
KR
答案 0 :(得分:0)
使用2.6.1版。这是2.6.0中的错误