将 Jackson 与 Jersey 一起使用时,JsonTypeInfo 不会序列化

发布于 2024-10-15 19:12:04 字数 1332 浏览 2 评论 0原文

我使用 JsonTypeInfo 注释了 JAXB 类,以便可以轻松序列化多态类。但是,当 Jersey 序列化时,注释不会显示。更具体地说,它在使用 ObjectMapper 时显示,但不作为资源的返回类型。我现在很困惑,因为这似乎是 Jersey => 的问题。杰克逊互动。

为了调试,我使用了 jersey-samples 中的 jsonfromjaxb 示例来定位我的问题。我将以下内容添加到 Flights 类中,使其序列化为 @class。

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")

我在资源中有以下可用方法,一种仅返回 JAXB 对象,另一种手动使用 ObjectMapper

@GET
@Produces({"application/json"})
public synchronized Flights getFlightList() {
    return myFlights;
}

@GET
@Path("/object_mapper")
@Produces({"application/json"})
public synchronized String getFlights() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(myFlights);
}

查询 /jsonfromjaxb/flights 的结果

{"flight":[{"flightId":"OK123","company":"Czech Airlines","number":123,"aircraft":"B737"},{"flightId":"OK124","company":"Czech Airlines","number":124,"aircraft":"AB115"}]}

查询 /jsonfromjaxb/flights/object_mapper 的结果

{"@class":"com.sun.jersey.samples.jsonfromjaxb.jaxb.Flights","flight":[{"number":123,"company":"Czech Airlines","aircraft":"B737","flightId":"OK123"},{"number":124,"company":"Czech Airlines","aircraft":"AB115","flightId":"OK124"}]}

谢谢, 赎金

I annotated a JAXB class with JsonTypeInfo so that I could serialize polymorphic classes easily. However, the annotation does not show up when serialized by Jersey. To be more specific, it shows up when using ObjectMapper but not as a return type from a resource. I am very confused right now as this seems to be a problem with Jersey => Jackson interaction.

To debug things, I used the jsonfromjaxb example from the jersey-samples to localize my problem. I added the following to the Flights class to have it serialize out to @class.

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")

I have the following methods available in the resource, one which just returns the JAXB object and one which manually uses ObjectMapper

@GET
@Produces({"application/json"})
public synchronized Flights getFlightList() {
    return myFlights;
}

@GET
@Path("/object_mapper")
@Produces({"application/json"})
public synchronized String getFlights() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(myFlights);
}

The result of querying /jsonfromjaxb/flights

{"flight":[{"flightId":"OK123","company":"Czech Airlines","number":123,"aircraft":"B737"},{"flightId":"OK124","company":"Czech Airlines","number":124,"aircraft":"AB115"}]}

The result of querying /jsonfromjaxb/flights/object_mapper

{"@class":"com.sun.jersey.samples.jsonfromjaxb.jaxb.Flights","flight":[{"number":123,"company":"Czech Airlines","aircraft":"B737","flightId":"OK123"},{"number":124,"company":"Czech Airlines","aircraft":"AB115","flightId":"OK124"}]}

Thanks,
Ransom

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

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

发布评论

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

评论(1

雪花飘飘的天空 2024-10-22 19:12:04

我认为您似乎没有使用基于 Jackson 的序列化(即使用 ObjectMapper 的序列化;低级 jackson 生成器用于大多数 JSON 输出,包括以不同方式完成绑定的输出)。如果是的话,它绝对应该看起来像您从显式使用中看到的那样。所以这似乎是改变 Jersey JSON 配置的问题。

I think it looks like you aren't using Jackson-based serialization (that is, one that uses ObjectMapper; low-level jackson generator is used for most JSON output, including ones where binding is done differently). If you were, it definitely should look like what you see from explicit use. So it seems to be matter of changing Jersey JSON configuration.

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