杰克逊使用JSON处理器,而无需使用说明 [英] Use Jackson JSON processor without using annotations

查看:282
本文介绍了杰克逊使用JSON处理器,而无需使用说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包,它仅仅包括和XSD文件生成(通过JAXB)共享类这是有关我们的服务器和客户端应用程序。因此,这些类包含XML注释。

I have a package which just includes and xsd file to generate (via JAXB) shared classes which are relevant for our server and client applications. So these classes contain XML annotations.

一个客户端是一个Android应用程序。我也想,因为我用它提供了JSON(与新泽西州REST服务进行通信在这里使用这些类的JSON反序列化/ 杰克逊)。

One client is an Android App. I also want to use these classes here for JSON deserialization because I communicate with a REST service which delivers JSON (with Jersey/Jackson).

我尝试在Android这样使用杰克逊(版本1.7.2):

I try to use Jackson (version 1.7.2) on the Android like this:

ObjectMapper mapper = new ObjectMapper();
mapper = mapper.configure(DeserializationConfig.Feature.USE_ANNOTATIONS,
                false);
mapper = mapper.configure(SerializationConfig.Feature.USE_ANNOTATIONS,
                false);
FooBar someObject = (FooBar) mapper.readValue(jsonString, FooBar.class);

我虽然配置映射器应避免使用注释,但我仍然获得

I though configure the MAPPER should avoid using annotations but I still get

02-07 09:30:18.631: ERROR/AndroidRuntime(447): java.lang.NoClassDefFoundError: javax.xml.bind.annotation.XmlAccessorType
02-07 09:30:18.631: ERROR/AndroidRuntime(447):     at java.lang.Class.getDeclaredAnnotations(Native Method)
02-07 09:30:18.631: ERROR/AndroidRuntime(447):     at org.codehaus.jackson.map.introspect.AnnotatedClass.resolveClassAnnotations(AnnotatedClass.java:292)
02-07 09:30:18.631: ERROR/AndroidRuntime(447):     at org.codehaus.jackson.map.introspect.AnnotatedClass.construct(AnnotatedClass.java:139)

谁知道的伎俩,从解决注释停止杰克逊。

Who knows the trick to stop Jackson from resolving annotations.

谢谢   克劳斯

推荐答案

我目前的调查导致这些结果:

My current investigations lead to these results:

DeserializationConfig.Feature.USE_ANNOTATIONS 属性的使用(设置为false)将导致杰克逊 DeserializerConfig 类使用 NopAnnotationIntrospector 。一类注释随后将使用该 NopAnnotationIntrospector 解决。该 NopAnnotationIntrospector 返回每个 isHandled 要求任何注释一个类 - 在进一步的处理,实际上不会使用此批注

The usage of the DeserializationConfig.Feature.USE_ANNOTATIONS property (set to false) will cause the JACKSON DeserializerConfig class to use a NopAnnotationIntrospector. Annotations of a class will then be resolved using this NopAnnotationIntrospector. The NopAnnotationIntrospector will return false on each isHandled request for any annotation on a class - and in fact will not use this annotation in further processing.

所以 - 系统仍然考察的注解 - 它必须要在这种情况下,类路径。由于Android不提供任何JAXB-API说明这导致了的NoClassDefFoundError

So - the system still "inspects" the annotations - which have to be on the Classpath in this case. As Android does not provide any jaxb-api annotations this leads to the NoClassDefFoundError.

我预计 USE_ANNOTATIONS = FALSE 将带来JACKSON完全忽视任何注解 - 但不幸的是事实并非如此。我现在使用的杰克逊流API来解析使用JACKSON数据绑定功能的JSON字符串代替。

I expected USE_ANNOTATIONS = false would bring JACKSON to totally ignore any annotations - but unfortunately it does not. I will now use the Jackson Streaming API to parse the JSON string instead of using JACKSON Data Binding capabilities.

这篇关于杰克逊使用JSON处理器,而无需使用说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆