使用带有 JAXB 注释的 Jackson JSON 库

发布于 2024-10-12 16:23:34 字数 712 浏览 4 评论 0原文

我正在尝试在基于 RESTEasy 的 Web 应用程序中启用 JAXB 注释支持,有人向我推荐了这篇文章 (http://wiki.fasterxml.com/JacksonJAXBAnnotations)。我能够获取 jackson-xc.jar,但我不知道如何注册注释内省器。目前,RESTEasy 自动序列化我的 JSON 响应,下面的内容适合哪里?目前,RESTeasy 自动序列化 JSON 对象。

包含 jackson-xc jar,其中包含 org.codehaus.jackson.xc.JaxbAnnotationIntrospector 注册这个注解内省器

    ObjectMapper mapper = new ObjectMapper();
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    // make deserializer use JAXB annotations (only)
    mapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
    // make serializer use JAXB annotations (only)
    mapper.getSerializationConfig().setAnnotationIntrospector(introspector);

I'm trying to enable JAXB annotation support within my RESTEasy based web application and this article was suggested to me (http://wiki.fasterxml.com/JacksonJAXBAnnotations). I'm able to get the jackson-xc.jar but I don't see how to register the annotation introspector. Currently, RESTEasy automatically serializes my JSON responses, where would the below fit? Currently, RESTeasy serializes the JSON object automatically.

Include jackson-xc jar, which contains org.codehaus.jackson.xc.JaxbAnnotationIntrospector
Register this annotation introspector

    ObjectMapper mapper = new ObjectMapper();
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    // make deserializer use JAXB annotations (only)
    mapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
    // make serializer use JAXB annotations (only)
    mapper.getSerializationConfig().setAnnotationIntrospector(introspector);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀞厅☆埖开 2024-10-19 16:23:34

你需要弄清楚的是如何让RESTeasy使用你已经配置的ObjectMapper。 JAX-RS 提供了通过提供者执行此操作的通用方法,但使用 RESTeasy 可能有更简单的方法来执行此操作。
一些 JAX-RS 实现还默认注册 JAXB 注释内省器。

最后一件事:您确定需要使用 JAXB 注释吗?虽然 Jackson 可以使用它们,但更好的方法是仅使用基本命名约定进行发现,并在需要覆盖的情况下使用 Jackson 自己的注释。 JAXB 的问题在于它是特定于 xml 的,因此与 JSON 一起使用时几乎没有阻力。

What you need to figure out is how to make RESTeasy use ObjectMapper you have configured. JAX-RS offers generic way to do this via providers, but there may be simpler way(s) to do this with RESTeasy.
Some JAX-RS implementations also register JAXB annotation introspector by default.

Last thing: are you sure you need to use JAXB annotations? While Jackson can use them, preferable method is to either just use basic naming convention for discovery and jackson's own annotations in case where overrides are needed. Problem with JAXB is that it is xml-specific so there is little bit of impedance when used with JSON.

空心空情空意 2024-10-19 16:23:34

感谢您的回复,我会看看您的建议...

我们想要坚持使用 jaxb 注释的原因是 @XmlRootElement(name="userResponse") 注释允许我们想要的额外包装元素我们的 JSON 响应。请参阅下面的示例:

我们希望有 {"response":{"userResponse":[{"user":{"businessRegion":"US","firstName":"joe","language ":"en","lastName":"smith"}}}]} 而不是 Jackson 当前输出的内容,{"response":[{"user":{"businessRegion":"US","firstName":"乔","语言":"en","姓氏":"史密斯"}}]}。是否有办法在不添加额外的包装类的情况下模仿 Jackson 中的 @XmlRootElement?

用户类别:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(名称=“userResponse”)
公共类 UserResponse 扩展 AbstractResponse{
私人用户用户;

Thanks for the reply, I'll take a look at your suggestions...

The reason why we want to stick with the jaxb annotations is that the @XmlRootElement(name="userResponse") annotation allows an extra wrapping element that we want in our JSON responses. See example below:

We want to have {"response":{"userResponse":[{"user":{"businessRegion":"US","firstName":"joe","language":"en","lastName":"smith"}}}]} instead of what Jackson currently outputs, {"response":[{"user":{"businessRegion":"US","firstName":"joe","language":"en","lastName":"smith"}}]}. Is there anyway to mimic the @XmlRootElement in Jackson without adding an additional wrapper class?

User class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="userResponse")
public class UserResponse extends AbstractResponse{
private Users user;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文