我可以编组List <! - ?使用Moxy(Jersey 2.17)将BaseObject - >扩展到json?

时间:2015-08-31 00:21:35

标签: json jersey moxy

泽西岛2.17与Moxy。

型号:

@XmlType(name="product.Product")
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Product {
}

@XmlType(name="product.PaperProduct")
@XmlAccessorType(XmlAccessType.FIELD)
public class PaperProduct extends Product {
}

@XmlType(name="product.WoodProduct")
@XmlAccessorType(XmlAccessType.FIELD)
public class WoodProduct extends Product {
}

找到PaperProduct的资源:

@GET
@Path("paperProduct")
public Response findPaperProducts() {
    List<PaperProduct> products = findService.findPaperProducts();
    GenericEntity genericEntity = new GenericEntity(products, new ParameterizedTypeImpl(List.class, PaperProduct.class));
    return Response.ok(genericEntity, responseMediaType).build();
}

Json结果(GOOD):

[ {
   "@xsi:type" : "product.PaperProduct",
   "name" : "note book",
}]

查找所有产品的资源,包括WoodProduct:

@GET
@Path("product")
public Response findAllProducts() {
    List<Product> products = findService.findProducts();
    GenericEntity genericEntity = new GenericEntity(products, new ParameterizedTypeImpl(List.class, Product.class));
    return Response.ok(genericEntity, responseMediaType).build();
}

Json结果(不完全是我想要的):

[ {
   "@xsi:type" : "product.Product",
   "name" : "note book"
}, {
   "@xsi:type" : "product.Product",
   "name" : "chair"}]

我想要的结果是:

[ {
   "@xsi:type" : "product.PaperProduct",
   "name" : "note book"
}, {
   "@xsi:type" : "product.WoodProduct",
   "name" : "chair"}]

如何获得我想要的结果?

0 个答案:

没有答案