dqan70724 2019-03-15 19:40
浏览 67

如何将JSON数组传递给API方法?

I am creating an API method that I want to send an array of custom JSON items to. I don't want to have a database model behind my JSON object. The JSON is just a way of sending transient data to my API method, and it's not directly backed by any database tables.

Here is the JSON data I'm posting:

{
   "stockItems": [
      {
        "sku": "SKU-1234",
        "qty": 15
      },
      {
        "sku": "SKU-5678",
        "qty": 25
      }
   ]
}

Here is my custom model and interface:

StockItemInterface.php

namespace MyCompany\CatalogInventory\Api\Data;

/**
 * @api
 */
interface StockItemInterface
{
    /**
     * @return float
     */
    public function getQty();

    /**
     * @return string
     */
    public function getSku();

    /**
     * @param float $qty
     * @return $this
     */
    public function setQty($qty);

    /**
     * @param string $sku
     * @return $this
     */
    public function setSku($sku);
}

Item.php

namespace MyCompany\CatalogInventory\Model\Stock;

use MyCompany\CatalogInventory\Api\Data\StockItemInterface;

class Item implements StockItemInterface
{
    protected $sku;

    protected $qty;

    /**
     * @param array $data
     */
    public function __construct(array $data = [])
    {
    }

    /**
     * @return float
     */
    public function getQty()
    {
        return $this->qty;
    }

    /**
     * @return string
     */
    public function getSku()
    {
        return $this->sku;
    }

    /**
     * @param float $qty
     * @return $this
     */
    public function setQty($qty)
    {
        $this->qty = $qty;
    }

    /**
     * @param string $sku
     * @return $this
     */
    public function setSku($sku)
    {
        $this->sku = $sku;
    }
}

Here's my API method

StockRegistryInterface.php

namespace MyCompany\CatalogInventory\Api;

/**
 * Interface StockRegistryInterface
 * @api
 */
interface StockRegistryInterface
{
    /**
    * @param \MyCompany\CatalogInventory\Api\Data\StockItemInterface[] $stockItems
    * @return int
    */
    public function updateStockItems(array $stockItems);
}

StockRegistry

namespace MyCompany\CatalogInventory\Model;

use Magento\Catalog\Model\ProductFactory;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface;
use MyCompany\CatalogInventory\Api\StockRegistryInterface;

/**
 * Class StockRegistry
 *
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class StockRegistry extends \Magento\CatalogInventory\Model\StockRegistry implements StockRegistryInterface
{
    /**
     * @param StockConfigurationInterface $stockConfiguration
     * @param StockRegistryProviderInterface $stockRegistryProvider
     * @param StockItemRepositoryInterface $stockItemRepository
     * @param StockItemCriteriaInterfaceFactory $criteriaFactory
     * @param ProductFactory $productFactory
     */
    public function __construct(
        StockConfigurationInterface $stockConfiguration,
        StockRegistryProviderInterface $stockRegistryProvider,
        StockItemRepositoryInterface $stockItemRepository,
        StockItemCriteriaInterfaceFactory $criteriaFactory,
        ProductFactory $productFactory
    ) {
        parent::__construct($stockConfiguration, $stockRegistryProvider, $stockItemRepository, $criteriaFactory, $productFactory);
    }

    /**
     * @inheritdoc
     */
    public function updateStockItems(array $stockItems)
    {
        // do stuff
    }
}

When I post to my API method, I get this error

"Fatal Error: 'Uncaught Error: Cannot instantiate interface MyCompany\\CatalogInventory\\Api\\Data\\StockItemInterface in 
\/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/ObjectManager\/Factory\/AbstractFactory.php:111    
Stack trace:
#0 \/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/ObjectManager\/Factory\/Compiled.php(108): 
Magento\\Framework\\ObjectManager\\Factory\\AbstractFactory->createObject('MyCompany\\\\CatalogI...', Array)
#1 \/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/ObjectManager\/ObjectManager.php(56): 
Magento\\Framework\\ObjectManager\\Factory\\Compiled->create('MyCompany\\\\CatalogI...', Array)
#2 \/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/Webapi\/ServiceInputProcessor.php(166): 
Magento\\Framework\\ObjectManager\\ObjectManager->create('\\\\MyCompany\\\\Catalog...')
#3 \/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/Webapi\/ServiceInputProcessor.php(399): 
Magento\\Framework\\Webapi\\ServiceInputProcessor->_createFromArray('\\\\MyCompany\\\\Catalog...', Array)
#4 \/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/Webapi\/ServiceInputProcessor.php(332): 
' in '\/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/ObjectManager\/Factory\/AbstractFactory.php' on line 111"

The exception is happening in AbstractFactory.createObject:

protected function createObject($type, $args)
{
    return new $type(...array_values($args));
}

The problem is that $args is an empty array, so array_values throws an exception.

How do I write my Item class so that Magento can construct it successfully, and without having a backing database model?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行