9

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;
}

そのリスト内のすべてのオブジェクトに対して「@class」フィールドを持つ Json 文字列を取得します。しかし、問題は、応答を返す場合:

@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」フィールドはありません。それはなぜですか、また、Response でリストを返す「@class」フィールドを持つリストを取得するにはどうすればよいですか? ちなみに、驚くべきことに、エンティティとして 1 つの Promotion オブジェクトのみを指定して Response を返し、その「@class」フィールドを取得すると機能します。

4

3 に答える 3

6

多分あなたは試してみたいでしょう:

GenericEntity<Collection<Promotion>> genericEntity = 
           new GenericEntity<Collection<Promotion>>(promotions){};
return Response.ok().entity(genericEntity).build();
于 2012-12-21T15:36:48.433 に答える