You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java通过WooCommerce API v2添加产品图片?报错解决

WooCommerce REST API v2 Java封装器上传产品报错修复

问题说明

使用Java版WooCommerce REST API v2封装器从CSV文件上传产品时,添加产品图片后触发两个错误:

  • stock_status参数值不在允许的枚举值(instock、outofstock、onbackorder)范围内
  • images参数类型错误,要求为数组而非单个对象

错误信息

错误输入

{regular_price=9.99, short_description=This is a short description, manage_stock=true, virtual=true, downloadable=true, images={id=0, url=https://redacted.com/assets/postcardicon.png}, stock_status=in_stock, name=Premium Quality, description=Pellentesque habitant morbi tristique senectus et netus, type=simple, sku=1234, stock_quantity=1}

错误输出

{code=rest_invalid_param, message=Invalid parameter(s): stock_status, images, data={status=400, params={stock_status=stock_status is not one of instock, outofstock, and onbackorder., images=images is not of type array.}, details={stock_status={code=rest_not_in_enum, message=stock_status is not one of instock, outofstock, and onbackorder., data=null}, images={code=rest_invalid_type, message=images is not of type array., data={param=images}}}}}

有效参考示例

有效输入(省略图片)

{regular_price=9.99, short_description=This is a short description, manage_stock=true, virtual=true, downloadable=true, stock_status=instock, name=Premium Quality, description=Pellentesque habitant morbi tristique senectus et netus, type=simple, sku=1234, stock_quantity=1}

有效输出(省略图片)

{id=40625, name=Premium Quality, slug=premium-quality, permalink=https://redacted.com/product/premium-quality/, date_created=2022-08-13T16:22:43, date_created_gmt=2022-08-13T16:22:43, date_modified=2022-08-13T16:22:43, date_modified_gmt=2022-08-13T16:22:43, type=simple, status=publish, featured=false, catalog_visibility=visible, description=Pellentesque habitant morbi tristique senectus et netus, short_description=This is a short description, sku=1234, price=9.99, regular_price=9.99, sale_price=, date_on_sale_from=null, date_on_sale_from_gmt=null, date_on_sale_to=null, date_on_sale_to_gmt=null, on_sale=false, purchasable=true, total_sales=0, virtual=true, downloadable=true, downloads=[], download_limit=-1, download_expiry=-1, external_url=, button_text=, tax_status=taxable, tax_class=, manage_stock=true, stock_quantity=1, in_stock=true, backorders=no, backorders_allowed=false, backordered=false, sold_individually=false, weight=, dimensions={length=, width=, height=}, shipping_required=false, shipping_taxable=false, shipping_class=, shipping_class_id=0, reviews_allowed=true, average_rating=0, rating_count=0, upsell_ids=[], cross_sell_ids=[], parent_id=0, purchase_note=, categories=[], tags=[], images=[{id=0, date_created=2022-08-13T16:22:44, date_created_gmt=2022-08-13T16:22:44, date_modified=2022-08-13T16:22:44, date_modified_gmt=2022-08-13T16:22:44, src=http://redacted.com/wp-content/plugins/woocommerce/assets/images/placeholder.png, name=Placeholder, alt=Placeholder, position=0}], attributes=[], default_attributes=[], variations=[], grouped_products=[], menu_order=0, price_html=<span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">$</span>9.99</bdi></span>, related_ids=[], meta_data=[], _links={self=[{href=https://leads.lionpridefinancial.com/wp-json/wc/v2/products/40625}], collection=[{href=https://redacted.com/wp-json/wc/v2/products}]}}}

问题代码

Map<String, Object> productInfo = new HashMap<>();
productInfo.put("name", "Premium Quality");
productInfo.put("type", "simple");
productInfo.put("description", "Pellentesque habitant morbi tristique senectus et netus");
productInfo.put("sku", "1234");
productInfo.put("short_description", "This is a short description");
productInfo.put("virtual", true);
productInfo.put("downloadable", true);
productInfo.put("regular_price", "9.99");
productInfo.put("stock_status", "in_stock");
productInfo.put("stock_quantity", "1");
productInfo.put("manage_stock", true);

productInfo.put("images", new HashMap <String, Object>() {{ 
        put("id",0);
        put("url","https://redacted.com/assets/postcardicon.png");
        }});
Map<String, Object> product = wooCommerce.create(EndpointBaseType.PRODUCTS.getValue(), productInfo);

    System.out.println(productInfo.toString());
    System.out.println(product.toString());

修复方案

针对两个错误点进行修正:

  1. 修正stock_status参数值:将"in_stock"改为枚举允许的"instock"
  2. 修正images参数类型:将单个HashMap对象改为包含该对象的List数组

修正后的代码

Map<String, Object> productInfo = new HashMap<>();
productInfo.put("name", "Premium Quality");
productInfo.put("type", "simple");
productInfo.put("description", "Pellentesque habitant morbi tristique senectus et netus");
productInfo.put("sku", "1234");
productInfo.put("short_description", "This is a short description");
productInfo.put("virtual", true);
productInfo.put("downloadable", true);
productInfo.put("regular_price", "9.99");
// 修正stock_status值为允许的枚举值
productInfo.put("stock_status", "instock");
productInfo.put("stock_quantity", "1");
productInfo.put("manage_stock", true);

// 修正images为数组类型,将图片对象放入List中
List<Map<String, Object>> imagesList = new ArrayList<>();
Map<String, Object> image = new HashMap<>();
image.put("id", 0);
image.put("url", "https://redacted.com/assets/postcardicon.png");
imagesList.add(image);
productInfo.put("images", imagesList);

Map<String, Object> product = wooCommerce.create(EndpointBaseType.PRODUCTS.getValue(), productInfo);

System.out.println(productInfo.toString());
System.out.println(product.toString());

内容的提问来源于stack exchange,提问作者Jose Garcia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 10:27:46