I have the following JSON as input to my custom API. But I can't set interface to get this done.
{
"locations": [
{
"location_id": 1000,
"location_name": "XYZ"
},
{
"location_id": 1001,
"location_name": "XYZ1"
}
]
}
I'm still getting the following error message:
Message: Property "0" does not have corresponding setter in class "Magecart\Productapi\Api\Data\LocationsInterface"
Please point me to where am doing wrong.
I tried following:
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;
}
}