gpt4 book ai didi

java - @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)在返回响应时不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:23:19 27 4
gpt4 key购买 nike

我正在使用 Jersey 编写 REST 服务。我有一个带有注释的抽象类 Promotion:

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)

因此,当我返回一个对象列表时:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("promotions/")
public List<Promotion> getClosestPromotions() {
List<Promotion> promotions = getPromotions(); //here I get some objects

return promotions;
}

我得到一个 Json 字符串,其中包含该列表中每个对象的“@class”字段。但问题是,如果我返回响应:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("promotions/")
public Response getClosestPromotions() {
List<Promotion> promotions = getPromotions(); //here I get some objects

return Response.ok().entity(promotions).build();
}

我得到几乎相同的列表,但没有额外的“@class”字段。为什么会这样,我该怎么做才能获得带有“@class”字段的列表,并在 Response 中返回一个列表?顺便说一句,令人惊讶的是,当我返回一个仅作为实体给出的 Promotion 对象的 Response 并且我得到那个“@class”字段时,它会起作用。

最佳答案

也许你想试试:

GenericEntity<Collection<Promotion>> genericEntity = 
new GenericEntity<Collection<Promotion>>(promotions){};
return Response.ok().entity(genericEntity).build();

关于java - @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)在返回响应时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13809444/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com