泽西岛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"}]
如何获得我想要的结果?