-2
Jersey ResourceConfigクラスを使用してREST Webサービスを作成しようとしています。リストタイプの不一致要素オブジェクト
しかし、私は理解できないエラーが発生しています。
型の不一致:製品への要素のObject型から変換することはできません
コード:
@Path("productcatalog")
public class ProductCatalogResource {
private static List productCatalog;
public ProductCatalogResource() {
initializeProductCatalog();
}
@GET
@Path("search/category/{category}")
@Produces(MediaType.APPLICATION_JSON)
public Product[] searchByCategory(@PathParam("category") String category) {
List products = new ArrayList();
for (Product p : productCatalog) { // OBJECT TYPE ERROR
if (category != null && category.equalsIgnoreCase(p.getCategory())) {
products.add(p);
}
}
return products.toArray(new Product[products.size()]); // OBJECT TYPE ERROR
}
private void initializeProductCatalog() {
if (productCatalog == null) {
productCatalog = new ArrayList();
productCatalog.add(new Product(id, name, category, unitPrice));
}
}
Productクラス:
@XmlRootElement
public class Product implements Serializable {
private int id;
private String name;
private String category;
private double unitPrice;
public Product() {} // needed for JAXB
public Product(int id, String name, String category, double unitPrice) {
this.id = id;
this.name = name;
this.category = category;
this.unitPrice = unitPrice;
}
}
生の種類は使用しないでください。 'private static List productCatalog'を' private static List productCatalog'に変更してください –
Eran
ありがとうございました。さらに、戻り値を変更しました。return(Product [])products.toArray(new Product [products.size()]); – ESDEE