我们的应用程序有一个包装 Jersey REST 功能的类。一种方法是public <T extends Storable> List<T> retrieve(Class<T[]> pCls) throws StorageException
,被称为retrieve(Item[].class)
从资源中获取整个项目列表/items
(见下文)。
来自服务器的答案是[{"price":1.0,"specialPrice":0.0,"name":"Beverage","special":false,"category":"BEVERAGE","cost":0.0,"available":false,"id":0},{"price":2.0,"specialPrice":0.0,"name":"Meal","special":false,"category":"DISH","cost":0.0,"available":false,"id":0}]
,这是我所期望的。
但应用程序以Exception in thread "main" javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException
, 并带有链接异常退出[com.sun.istack.internal.SAXParseException2; lineNumber: 1; columnNumber: 14; unexpected element (uri:"", local:"price"). Expected elements are <{}article>,<{}item>]
。
检索代码(客户端):
ClientResponse cr = this.resource.path(resourcePath)
.accept(MediaType.APPLICATION_JSON_TYPE)
.get(ClientResponse.class);
T[] a = cr.getEntity(pCls);
资源代码(在服务器上):
@Path("/items")
public class ItemListResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response getItemList() {
LinkedList<Item> l = new LinkedList<Item>();
l.add(new Item(1.0, "Beverage", Category.BEVERAGE));
l.add(new Item(2.0, "Meal", Category.DISH));
return Response.ok(l).build();
}
}
项目代码:
@XmlRootElement
public class Item extends Article {
private Category category;
private double cost;
private boolean available;
};