Magento – REST Api interface di not working. Class ItemInterface does not exist

apidiinterface

ItemInterface:

namespace MyVendor\SampleModule1\Api\Data;

interface ItemInterface
{
    /**
     * @return string
     */
    public function getName();

    /**
     * @return string|null
     */
    public function getDescription();
}

ItemRepositoryInterface:

<?php

namespace MyVendor\SampleModule1\Api;

use MyVendor\SampleModule1\Api\Data\ItemInterface;

interface ItemRepositoryInterface
{
    /**
     * @return ItemInterface[]
     */
    public function getList();
}

ItemRepository:

<?php

namespace MyVendor\SampleModule1\Model;

use MyVendor\SampleModule1\Api\ItemRepositoryInterface;
use MyVendor\SampleModule1\Model\ResourceModel\Item\CollectionFactory;

class ItemRepository implements ItemRepositoryInterface
{
    private $collectionFactory;

    public function __construct(CollectionFactory $collectionFactŠ¾ry)
    {
        $this->collectionFactory = $collectionFactŠ¾ry;
    }

    public function getList()
    {
        return $this->collectionFactory->create()->getItems();
    }
}

etc/di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Console\CommandList">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="myvendorAddItem" xsi:type="object">MyVendor\SampleModule1\Console\Command\AddItem</item>
            </argument>
        </arguments>
    </type>
    <preference for="MyVendor\SampleModule1\Api\Data\ItemInterface" type="MyVendor\SampleModule1\Model\Item"/>
    <preference for="MyVendor\SampleModule1\Api\ItemRepositoryInterface" type="MyVendor\SampleModule1\Model\ItemRepository"/>
</config>

etc/webapi.xml

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/myvendor" method="GET">
        <service class="MyVendor\SampleModule1\Api\ItemRepositoryInterface" method="getList" />
        <resources>
            <resource ref="anonymous" />
        </resources>
    </route>
</routes>

I ran the commands:

magento cache:flush
magento cache:clean
magento setup:di:compile
magento setup:upgrade

When i access the url http://mymage.com/rest/V1/myvendor i get the fallowing error:

<message>Class ItemInterface does not exist</message>
<code>-1</code>
<trace>
#0 /var/www/html/lib/internal/Magento/Framework/Reflection/MethodsMap.php(149): ReflectionClass->__construct('ItemInterface') #1.... 

EDIT:

Item.php model:

<?php

namespace MyVendor\SampleModule1\Model;

use Magento\Framework\Model\AbstractModel;

class Item extends AbstractModel
{
    protected function _construct()
    {
        $this->_init(\MyVendor\SampleModule1\Model\ResourceModel\Item::class);
    }
}

I just don't understand where is the problem ? ? ?

Best Answer

So, basically the ItemRepositoryInterface should look like:

<?php

namespace MyVendor\SampleModule1\Api;

interface ItemRepositoryInterface
{
    /**
     * @return \MyVendor\SampleModule1\Api\Data\ItemInterface[]
     */
    public function getList();
}

... I have no words .