I am trying to create an API in Magento2 mine custom Module. But its giving 500 : {"message":"Invalid parameter type \"\\Netzwelt\\Booking\\Model\\BookingLocationList\"
error, its coming when as I m opening swagger.
Here is mine code –
app\code\Netzwelt\Booking\Api\BookingLocationListInterface.php
namespace Netzwelt\Booking\Api;
interface BookingLocationListInterface
{
/**
* @return \Netzwelt\Booking\Model\BookingLocationList
*/
public function getBookingLocationList();
}
app\code\Netzwelt\Booking\Model\BookingLocationList.php
namespace Netzwelt\Booking\Model;
use Magento\Framework\ObjectManagerInterface;
class BookingLocationList extends \Magento\Framework\Model\AbstractModel
{
protected $objectmanager;
public function __construct(
ObjectManagerInterface $objectmanager
) {
$this->_objectManager = $objectmanager;
}
public function getBookingLocationList(){
$data[] = 1;
$data[] = 2;
$data[] = 3;
$data[] = 4;
return $data;
}
}
app\code\Netzwelt\Booking\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 method="GET" url="/V1/netzwelt_booking">
<service class="Netzwelt\Booking\Api\BookingLocationListInterface" method="getBookingLocationList"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
app\code\Netzwelt\Booking\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="Netzwelt\Booking\Api\BookingLocationListInterface" type="Netzwelt\Booking\Model\BookingLocationList" />
</config>
Please check and let me know what I am doing wrong?
Best Answer
di.xml
file which will be atNetzwelt/Booking/etc/
and code should be like this.you may need run this command
php bin/magento setup:di:compile
andphp bin/magento cache:flush
as we worked with XML.In PHP try this code for calling.
Please correct doc like this.
use Magento\Framework\ObjectManagerInterface;