@XmlJavaTypeAdapter 与 @JsonSerialize [使用restEasy/Jersey/Jackson/Mule]
我对所有 JAXB/JAX-RS 内容都很陌生。在工作中,我们使用 Mule ESB 及其 Jersey/Jackson 模块来接受传入的 REST 请求。在客户端,我们使用 RestEasy(与 Jackson)...请求应该采用 Json,而不是 XML。
它适用于仅包含字符串字段的简单对象。但是,一旦我们开始使用更“复杂”的类型,例如 Locales、enums 或 Maps,问题就会出现。
如果我理解正确的话,JAXB 中没有针对像 Map 这样的类型的内置序列化器(我什至应该说“HashMap”,因为如果我理解正确的话,JAXB 不能很好地支持接口)。因此,您必须为这些字段提供自己的序列化器。
从我在网络上的搜索中,我发现 @XmlJavaTypeAdapter 注释可用于管理您自己的“Map”等类型的序列化。但我还看到,在 Jackson 文档中,可以使用 @JsonSerialize(using=MySerializer.class) 注释。
我们应该使用什么以及为什么? @XmlJavaTypeAdapter 还是@JsonSerialize?它们是相同的还是有不同的目的?
I'm new to all the JAXB/JAX-RS stuff. At work, we are using Mule ESB with its Jersey/Jackson module to accept incoming REST requests. On the client side we're using RestEasy (with Jackson)... The request should be in Json, not in XML.
It works well for simple objects, containing String fields only. But as soon as we start using more "complex" types, like Locales, enums or Maps, problems arise.
If I understand correctly, there is no built-in serializers in JAXB for types like Map (I should even say "HashMap", since JAXB doesn't support interfaces well if I understand correctly). So you have to provide your own serializers for those fields.
From my searches on the web, I've seen that the @XmlJavaTypeAdapter annotation can be used to manage your own serialization of a type like "Map". But I also see, in Jackson documentation, that the @JsonSerialize(using=MySerializer.class) annotation can be used.
What should we use and why? @XmlJavaTypeAdapter or @JsonSerialize? Are they the same or do they have different purpose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先:Jackson 完全支持所有 JDK 结构化类型,包括 Map,因此通常不需要特殊配置或注释。 Jackson 不是 JAXB 实现,也不受 JAXB 限制(这是基于 POJO 和 XML 之间的映射问题,这
比 JSON 的大一些)。
Jackson也很好地支持接口;对于序列化,您很少需要任何额外的东西。
对于反序列化,您可能需要也可能不需要;具体实现方式有多种;但您通常不需要定义自定义序列化器或反序列化器。
因此,答案可能是您都不需要。
现在:JAXB 支持对于 Jackson 来说是一个可选的兼容性,主要是为了支持遗留的东西,其中 JAXB 是或曾经用于 XML 的东西。如果您不从 JAXB 注释开始,Jackson 自己的注释是首选机制。
因此,除非您需要 JAXB 注释用于其他目的(读取/写入 XML),否则我建议仅使用 Jackson 的 @JsonSerialize(如果确实需要基于注释的配置)。
First of all: Jackson fully supports all JDK structured types, including Maps, so no special configuration or annotations are usually needed. Jackson is not a JAXB implementation or limited by JAXB limitations (which are based on mapping problems between POJO and XML, which
are somewhat bigger than those with JSON).
Jackson also supports interfaces well; for serialization you seldom need anything additional.
For deserialiation you may or may not need; and there are multiple ways to indicate concrete implementations; but you usually do not need to define custom serializers or deserializers.
So it is possible that answer is that you do not need either.
Now: JAXB support is an optional compatibility thing for Jackson, mostly meant for supporting legacy things where JAXB is or was the thing used for XML. Jackson's own annotations are the preferred mechanism, if you do not start with JAXB annotations.
So unless you need JAXB annotations for other purposes (reading/writing XML), I would suggest just using Jackson's @JsonSerialize, if annotation-based configuration is indeed needed.