kra*_*jol 7 java rest json jersey jackson
我正在使用Jersey编写REST服务.我有一个带有注释的抽象类Promotion:
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
Run Code Online (Sandbox Code Playgroud)
多亏了这一点,当我返回一个对象列表时:
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("promotions/")
public List<Promotion> getClosestPromotions() {
List<Promotion> promotions = getPromotions(); //here I get some objects
return promotions;
}
Run Code Online (Sandbox Code Playgroud)
我为该列表中的每个对象获取一个带有"@class"字段的Json字符串.但问题是如果我返回一个Response:
@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();
}
Run Code Online (Sandbox Code Playgroud)
我得到几乎相同的列表,但没有额外的"@class"字段.为什么这样,我该怎么做才能获得一个带有"@class"字段的列表在Response中返回一个列表?顺便说一下,令人惊讶的是,当我返回一个只有作为实体给出的一个Promotion对象的Response并且我得到那个"@class"字段时它才起作用.
小智 5
也许你想尝试:
GenericEntity<Collection<Promotion>> genericEntity =
new GenericEntity<Collection<Promotion>>(promotions){};
return Response.ok().entity(genericEntity).build();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8820 次 |
最近记录: |