Как настроить интерфейсы для создания пользовательского API модуля в Magento2


У меня есть следующий JSON в качестве входных данных для моего пользовательского API. Но я не могу установить интерфейс , чтобы это сделать.

{
    "locations": [
        {
            "location_id": 1000,
            "location_name": "XYZ"
        },
        {
            "location_id": 1001,
            "location_name": "XYZ1"
        }
    ]
}

Я все еще получаю следующее сообщение об ошибке:

Сообщение: Свойство "0" не имеет соответствующего задатчика в классе "Magecart\Productapi\Api\Data\Locationsinterface"

Пожалуйста, укажите мне, где я делаю неправильно.

Я пытался следующее:


App\code\Test\Productapi\etc\webapi.xml

<route url="/V1/location" method="POST">
    <service class="Magecart\Productapi\Api\ProductapiInterface" method="postLocation"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
</route>

App\code\Magecart\Productapi\etc\di.xml

<preference for="Magecart\Productapi\Api\ProductapiInterface" type="Magecart\Productapi\Model\Productapi" />
<preference for="Magecart\Productapi\Api\Data\LocationInterface" type="Magecart\Productapi\Model\Location" />
<preference for="Magecart\Productapi\Api\Data\LocationsInterface" type="Magecart\Productapi\Model\Locations" />

App\code\Magecart\Productapi\Api\ProductapiInterface.php

namespace Magecart\Productapi\Api;

use Magecart\Productapi\Api\Data\LocationInterface;
use Magecart\Productapi\Api\Data\LocationsInterface;

interface ProductapiInterface
{
    /**
     * get locations.
     *
     * @param \Magecart\Productapi\Api\Data\LocationsInterface[] $locations
     * @return \Magecart\Productapi\Api\Data\LocationsInterface
     */
    public function postLocation(LocationsInterface[] $locations);
}

App\code\Magecart\Productapi\Model\Productapi.php

namespace Magecart\Productapi\Model;

use Magecart\Productapi\Api\ProductapiInterface;

class Productapi implements ProductapiInterface
{
    public function postLocation(\Magecart\Productapi\Api\Data\LocationsInterface $location) {
        //code
    }
}

App\code\Magecart\Productapi\Api\Data\LocationsInterface.php

namespace Magecart\Productapi\Api\Data;
/**
 * @api
 */
interface LocationsInterface
{
    /**
     * Gets list location
     *
     * @return \Magecart\Productapi\Api\Data\LocationInterface|null
     */
    public function getLocations();

    /**
     * Sets list location
     *
     * @param \Magecart\Productapi\Api\Data\LocationInterface $locations
     * @return $this
     */
    public function setLocations($locations = null);
}

App\code\Magecart\Productapi\Model\Locations.php

namespace Magecart\Productapi\Model;

use Magecart\Productapi\Api\Data\LocationsInterface;

class Locations implements LocationsInterface
{
    /**
     * Gets list of product location
     *
     * @return \Magecart\Productapi\Api\Data\LocationInterface|null
     */
    public function getLocations()
    {
        return $this;
    }

    /**
     * Sets list of product location
     *
     * @param \Magecart\Productapi\Api\Data\LocationInterface[] $location
     * @return $this
     */
    public function setLocations($location = null)
    {
        return $location;
    }
}

App\code\Magecart\Productapi\Api\Data\LocationInterface.php

namespace Magecart\Productapi\Api\Data;
/**
 * @api
 */
interface LocationInterface
{
    /**
     * Get location_id.
     *
     * @return int|null
     */
    public function getLocationId();

    /**
     * Set location_id.
     *
     * @param int $locationId
     * @return $this
     */
    public function setLocationId($locationId);

    /**
     * Get location_name.
     *
     * @return string|null
     */
    public function getLocationName();

    /**
     * Set location_name.
     *
     * @param string $locationName
     * @return $this
     */
    public function setLocationName($locationName);
}

App\code\Magecart\Productapi\Model\Location.php

namespace Magecart\Productapi\Model;

use Magecart\Productapi\Api\Data\LocationInterface;

class Location implements LocationInterface
{
    private $locationId;
    private $locationName;

    public function __construct() {
        $this->locationId = 0;
        $this->locationName = '';
    }

    /**
     * Get the locationId.
     *
     * @api
     * @return int The locationId.
     */
    public function getLocationId() {
        return $this->locationId;
    }

    /**
     * Set the locationId.
     *
     * @api
     * @param $value int The new locationId.
     * @return null
     */
    public function setLocationId($value) {
        $this->locationId = $value;
    }

    /**
     * Get the locationName.
     *
     * @api
     * @return string The locationName.
     */
    public function getLocationName() {
        return $this->locationName;
    }

    /**
     * Set the locationName.
     *
     * @api
     * @param $value string The new locationName.
     * @return null
     */
    public function setLocationName($value) {
        $this->locationName = $value;
    }
}
Author: 7ochem, 2017-02-17

1 answers

Из вашего вопроса Свойство "0" не имеет соответствующего задатчика в классе "Magecart\Productapi\Api\Данные\Locationsinterface"

Вы не можете отправить массив на самом первом объекте.

Требуется имя поля.

Вам нужно изменить свои входные данные, например:

{ "locations":{ "locations": [ { "location_id": 1000, "location_name": "XYZ" }, { "location_id": 1001, "location_name": "XYZ1" } ] } }

Сообщение: Свойство "0" не имеет соответствующего задатчика в классе "Magecart\Productapi\Api\Data\Locationsinterface"

Тогда эта ошибка будет удалено.


Затем имейте попробуйте

App\code\Test\Productapi\etc\webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/location" method="POST">
        <service class="Test\Productapi\Api\ProductapiInterface" method="postLocation"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

App\code\Test\Productapi\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">
    <preference for="Test\Productapi\Api\ProductapiInterface" type="Test\Productapi\Model\Productapi" />
    <preference for="Test\Productapi\Api\Data\LocationInterface" type="Test\Productapi\Model\Location" />
    <preference for="Test\Productapi\Api\Data\LocationsInterface" type="Test\Productapi\Model\Locations" />
</config>

App\code\Test\Productapi\Api\ProductapiInterface.php

namespace Test\Productapi\Api;
    use Test\Productapi\Api\Data\LocationInterface;
    use Test\Productapi\Api\Data\LocationsInterface;

    interface ProductapiInterface
    {
        /**
         * get locations.
         *
         * @param \Test\Productapi\Api\Data\LocationsInterface $locations
         * @return \Test\Productapi\Api\Data\LocationsInterface
         */
        public function postLocation($locations);
    }

App\code\Test\Productapi\Api\Data\LocationsInterface.php

namespace Test\Productapi\Api\Data;
/**
 * @api
 */
interface LocationsInterface
{
    /**
     * Gets list location
     *
     * @return \Test\Productapi\Api\Data\LocationInterface|null
     */
    public function getLocations();

    /**
     * Sets list location
     *
     * @param \Test\Productapi\Api\Data\LocationInterface[] $locations
     * @return $this
     */
    public function setLocations(array $locations = null);
}

App\code\Test\Productapi\Api\Data\LocationInterface.php

namespace Test\Productapi\Api\Data;
    /**
     * @api
     */
    interface LocationInterface
    {
        /**
         * Get location_id.
         *
         * @return int|null
         */
        public function getLocationId();

        /**
         * Set location_id.
         *
         * @param int $locationId
         * @return $this
         */
        public function setLocationId($locationId);

        /**
         * Get location_name.
         *
         * @return string|null
         */
        public function getLocationName();

        /**
         * Set location_name.
         *
         * @param string $locationName
         * @return $this
         */
        public function setLocationName($locationName);
    }

App\code\Test\Productapi\Model\Productapi.php

namespace Test\Productapi\Model;

use Test\Productapi\Api\ProductapiInterface;

class Productapi implements ProductapiInterface
{
    public function postLocation($location) {
        var_dump($location->getLocations());
        //your results will get
        //code
    }
}

app\code\Test\Productapi\Model\Locations.php

namespace Test\Productapi\Model;

use Test\Productapi\Api\Data\LocationsInterface;

class Locations implements LocationsInterface
{
   private $locations;

    /**
     * Gets list of product location
     *
     * @return \Test\Productapi\Api\Data\LocationInterface|null
     */
    public function getLocations()
    {
        return $this->locations;
    }

    /**
     * Sets list of product location
     *
     * @param \Test\Productapi\Api\Data\LocationInterface[] $location
     * @return $this
     */
    public function setLocations(array $location = null)
    {
        $this->locations = $locations;
    }
}

App\code\Test\Productapi\Model\Location.php

namespace Test\Productapi\Model;

use Test\Productapi\Api\Data\LocationInterface;

class Location implements LocationInterface
{
    private $locationId;
    private $locationName;

    public function __construct() {
        $this->locationId = 0;
        $this->locationName = '';
    }

    /**
     * Get the locationId.
     *
     * @api
     * @return int The locationId.
     */
    public function getLocationId() {
        return $this->locationId;
    }

    /**
     * Set the locationId.
     *
     * @api
     * @param $value int The new locationId.
     * @return null
     */
    public function setLocationId($value) {
        $this->locationId = $value;
    }

    /**
     * Get the locationName.
     *
     * @api
     * @return string The locationName.
     */
    public function getLocationName() {
        return $this->locationName;
    }

    /**
     * Set the locationName.
     *
     * @api
     * @param $value string The new locationName.
     * @return null
     */
    public function setLocationName($value) {
        $this->locationName = $value;
    }
}

Примечание: обновлено несколько файлов, я надеюсь, что это поможет вам удовлетворить ваши потребности.

Спасибо

 2
Author: Sathish, 2017-02-18 02:17:23