- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 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/
当尝试将通用对象序列化/反序列化为特定类时,我遇到了以下问题: 代码: // Generic holding the payload data linked to this event @JsonTy
我正在使用 Jersey 编写 REST 服务。我有一个带有注释的抽象类 Promotion: @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS) 因此,当我返回一个
所以我的 JSON 看起来像这样: { "ActivityDisplayModel" : { "name" : "lunch with friends", "s
我正在尝试使用 Jackson 2.9.8 序列化/反序列化多态类型,除非我将此类类型的对象放入集合中,否则它可以正常工作,因为由于某种原因,当时没有写入类型信息。让我们考虑以下示例: @JsonIg
使用 Spring 3 和 Jackson 1.7.6,我可以序列化抽象类的实现并将类的完全限定名称输出为名为 @class 的属性。 .当我的 Spring Controller 从用 @Respo
使用这些依赖 compile "com.fasterxml.jackson.core:jackson-annotations:2.8.8" compile "org.codehaus.jackson:
我正在使用以下依赖项进行 JSON 序列化/反序列化 com.fasterxml.jackson.core jackson-databind 2.7.4 com.
我正在尝试序列化和反序列化(使用 Json 和 ObjectMapper )在基类上具有以下注释的对象的多态列表: @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, i
我正在尝试使用 Jackson 来序列化具有多态性的实体。序列化的 JSON 字符串应该包含一个附加的“type”属性,其中“groupA”或“groupB”作为值,但它没有。我的实体如下所示: @E
friend 们, Jackson 框架提供基于注释的方法来在序列化过程中发出类型信息。 我不想使用 @JsonTypeInfo 我的任何类(class)中的注释。 以上注释是否有任何替代方案。 如果
我发现 Jackson JSON Processor 库有一些奇怪的行为,我很好奇这是故意的还是错误。请看下面的代码: @JsonTypeInfo(use = Id.NAME) public clas
我正在使用 @JsonTypeInfo 指示 Jackson 在 @class 属性中查找具体类型信息。但是,有时我不想指定 @class,特别是当可以根据上下文推断出子类型时。 最好的方法是什么?
我的父类(super class)型注释为 @JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "_typeid") - 以便
我有这样的回应: { "id":"decaa828741611e58bcffeff819cdc9f", "statement":"question statement", "
我有类层次结构 A <- B <- C。我不希望类 A 知道 C,所以我打算使用 A.type="B"表示它是类 B,然后是 B.type2 ="C"表示它是 C 类。 @JsonTypeInfo(u
我的目标是使用 Jackson 将 JSON 字符串字段转换为正确的类。 我有以下类(class): public class AnimalRecord { private String id
我正在使用 JsonTypeInfo 来处理系统读入的一些 JSON 对象的多态性。系统还将这些对象提供给其他服务。在某些情况下,我想要包括类型信息的详细对象,而在其他情况下,我想要对象的准系统最小
我尝试在应用程序的客户端序列化一个对象(来自 Enum ),并使用 jackson 在另一端(服务器端)将其反序列化为同一对象。我的客户端有这个枚举: @JsonTypeInfo(use = Json
假设我有一些像这样的 JSON [ { 'type' : { 'value': 'B' } }, {
我遇到了一种情况,我需要自定义某些 JSON 的序列化/反序列化。我已将其简化为一个可读的示例。我有一个 Container 类,其中包含实现 MyInterface 的对象。在我的示例中,Class
我是一名优秀的程序员,十分优秀!